Linux下C++语言统计一段逻辑处理耗费时间的方法

2015年04月28日 其他资源 5条评论 阅读5200次

Linux下C++语言统计一段逻辑处理耗费时间的方法

Linux下C++语言计算一段逻辑处理耗费时间的方法[微妙级]

有一段逻辑处理,想要知道它所消耗的时间用以计算性能,所以要在处理开始前,处理结束后分别取时间来计算中间处理消耗的时间。

考虑到C++本身的高性能,一般的逻辑耗时也不会太多,所以这时我们希望计算的处理时间能够精确到微妙级别。

struct timeval start, end, diff;

gettimeofday(&start, 0);

// your logic

// ......

gettimeofday(&end, 0);

int nsec;

diff.tv_sec = ( end.tv_sec - start.tv_sec );

diff.tv_usec = ( end.tv_usec - start.tv_usec );

if (diff.tv_usec < 0)

{

    diff.tv_sec--;

    diff.tv_usec += 1000000;

}

int usec = diff.tv_usec + diff.tv_sec * 100000;

printf("logic used time %d(us)", usec);

分享本文至:

WRITTEN BY

avatar
本文标签:C++性能
看了本文是不是觉得很赞,那就赶紧点击下面按钮分享给身边的朋友吧!

5 条评论

  1. avatar avonbeads

    <a href="http://www.bluesophy.com/greg-olsen-jersey-shirt-nflh">greg olsen jersey shirt</a> <a href="http://www.stylehousestl.com/adidas-ace-16-oro-and-blanco-zapatosh">adidas ace 16 oro and blanco</a> <a href="http://www.byvergara.com/kvinners-nike-zoom-structure-18-all-hvit-scarpea">kvinners nike zoom structure 18 all hvit</a> <a href="http://www.ruthlesscrew.com/washington-state-cougars-nike-ncaa-classic-swoosh-cap-hati">washington state cougars nike ncaa classic swoosh cap</a> <a href="http://www.fullserviceap.com/nike-zoom-train-action-gris-noir-trainersb">nike zoom train action gris noir</a> <a href="http://www.idealkizlar.com/herren-nike-roshe-run-hyp-orange-gr%C3%BCn-kurpesd">herren nike roshe run hyp orange gr眉n</a>
    avonbeads http://www.avonbeads.com/

  2. avatar drpfanstiel

    <a href="http://www.bazicloob.com/nike-air-max-90-hyperfuse-fluorescent-green-shoesk">nike air max 90 hyperfuse fluorescent green</a> <a href="http://www.malatyabolge.com/adidas-climacool-ride-ii-menn-svart-oransje-obuvi">adidas climacool ride ii menn svart oransje</a> <a href="http://www.impeachjohn.com/nike-blazer-high-hvit-r%C3%B8d-obuvk">nike blazer high hvit r酶d</a> <a href="http://www.dafuqstory.com/nike-free-run-2-rosa-and-gr%C3%A5-obuvi">nike free run 2 rosa and gr氓</a> <a href="http://www.liberezmoi.com/hombres-air-foamposite-one-blanco-negro-gris-zapatosl">hombres air foamposite one blanco negro gris</a> <a href="http://www.scrutinit.com/nero-bandana-nike-cortez-scarpeh">nero bandana nike cortez</a>
    drpfanstiel http://www.drpfanstiel.com/

  3. avatar coachpsy

    <a href="http://www.trancelater.net/nike-roshe-two-triple-nero-kurpesc">nike roshe two triple nero</a> <a href="http://www.uscampvictory.net/reebok-insta-pump-fury-wei%C3%9F-gold-schuhea">reebok insta pump fury wei脽 gold</a> <a href="http://www.veetracker.net/air-jordan-retro-8-negro-naranja-marmalade-kurpesf">air jordan retro 8 negro naranja marmalade</a> <a href="http://www.velomini.net/ua-clutchfit-prodigy-silber-chaussuresa">ua clutchfit prodigy silber</a> <a href="http://www.voilahalla.net/adidas-john-wtodas-verde-inferno-kurpesg">adidas john wtodas verde inferno</a> <a href="http://www.walterinsurance.net/nike-free-run-2-schwarz-metalleic-silber-lab-schuhee">nike free run 2 schwarz metalleic silber lab</a>
    coachpsy http://www.coachpsy.net/

  4. avatar keleric

    <a href="http://www.koreazp.com/womens-nike-lunarglide-7-gold-orange-shoesi">womens nike lunarglide 7 gold orange</a> <a href="http://www.hiltonskyshop.com/nike-kobe-5-yellow-red-shoesp">nike kobe 5 yellow red</a> <a href="http://www.lavaforchange.com/air-jordan-b-mo-rosa-lilla-obuvl">air jordan b mo rosa lilla</a> <a href="http://www.lifeonfilms.com/air-jordan-phase-mintgr%C3%B8n-gr%C3%A5-skoj">air jordan phase mintgr酶n gr氓</a> <a href="http://www.midlandroofer.com/femminile-nike-air-max-tailwind-6-blu-oro-scarpel">femminile nike air max tailwind 6 blu oro</a> <a href="http://www.pearsonchool.com/womens-asics-gel-noosa-tri-8-yellow-white-shoesj">womens asics gel noosa tri 8 yellow white</a>
    [url=http://www.keleric.com/]keleric[/url]

  5. avatar transonellc

    <a href="http://www.joshuaarens.com/maschio-nike-air-huarache-bianca-verde-butyg">maschio nike air huarache bianca verde</a> <a href="http://www.jschwimmermd.com/youth-nike-new-york-jets-51-brandon-copeland-limited-black-2016-salute-to-service-nfl-jersey-nflx">youth nike new york jets 51 brandon copeland limited black 2016 salute to service nfl jersey</a> <a href="http://www.markedtreear.com/air-jordan-retro-9-gamma-azul-butyd">air jordan retro 9 gamma azul</a> <a href="http://www.mauigrub.com/nike-free-5.0-femmes-noir-gray-kurpesc">nike free 5.0 femmes noir gray</a> <a href="http://www.mgnmilitaria.com/ralph-lauren-shirt-polo-wei%C3%9F-in-slim-fit-m%C3%A4nner-cotton-twill-polos">ralph lauren shirt polo wei脽 in slim fit m盲nner cotton twill</a> <a href="http://www.nanainchief.com/mujeres-nike-air-huarache-light-butyf">mujeres nike air huarache light</a>
    transonellc http://www.transonellc.com/

欢迎留言




用户登录

sitemap