绝对不要使用gettimeofday()来测量时间

2015年06月23日 其他资源 16条评论 阅读26553次

前段时间写了一篇在Linux下C++开发中想要测量一段逻辑处理所花费的时间的文章,当时我是使用的gettimeofday()方法,但文章发布之后,发现谷歌广告相关链接却是gettimeofday() should never be used to measure time的广告,并且在其他网站上也见到了这个广告链接。这种技术性很强的广告链接的确很少见的。

点开广告内容,便是https://blog.habets.se/2010/09/gettimeofday-should-never-be-used-to-measure-time

先不去讨论这个广告的意图,暂且从上述链接文章把大体摘要一些信息,再仔细考虑下是否合理。

声明下,从英语原文中省略了很多东西,没有全翻译,只是把主要段落贴出来。

gettimeofday() 和 time()应当只被用来得到当前你想要的时钟时间,绝不能用来测量时间或者安排某个事件在将来某个时间去做。

出了什么问题?


gettimeofday()返回的是当前时钟时间和时区。time()返回的是包含时间秒,但不包含时区的一个子集信息。

调用这两个函数来测量一段逻辑处理所占用的时间好像是很容易被人接受的。毕竟,在现实生活中,我们也是通过记录手表的开始时间和结束时间来计算花费时间的。

应该用什么替代gettimeofday()


最简单的来准备测量时间应该用clock_gettime(CLOCK_MONOTONIC, ...)方法。

如果重启,它将不稳定,不过我们也不在意这点。我们只是想用一个计时器来测量一段逻辑处理花费的时间。

timespec start, end, diff;
clock_gettime(CLOCK_THREAD_CPUTIME_ID, &start);
/// your logic here
clock_gettime(CLOCK_THREAD_CPUTIME_ID, &end);
diff.tv_sec = ( end.tv_sec - start.tv_sec );
diff.tv_nsec = ( end.tv_nsec - start.tv_nsec );
if (diff.tv_nsec < 0) {
    diff.tv_sec--;
    diff.tv_nsec += 1000000000;
}
int usec = diff.tv_nsec + diff.tv_sec * 100000000;
double resultTime = (double)usec / 1000000;
printf("TimeMeasure: used time %6.1lf[ms]\n", resultTime);


到此,摘要地基本差不多了。

概要地说,就是要用clock_gettime(,)来代替gettimeofday()

分享本文至:

WRITTEN BY

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

16 条评论

  1. <a href="http://www.daddeaze.com/houston-rockets-yao-ming-11-red-swingman-jersey-nflc">houston rockets yao ming 11 red swingman jersey</a> <a href="http://www.debracousins.com/nike-free-5.0-herren-rot-rosa-runningf">nike free 5.0 herren rot rosa</a> <a href="http://www.yankaripark.com/nfl-pittsburgh-steelers-color-rush-limited-antonio-brown-mens-football-jersey-nflv">nfl pittsburgh steelers color rush limited antonio brown mens football jersey</a> <a href="http://www.yicailottery.com/cheap-nike-roshe-black-plain-black-trainersa">cheap nike roshe black plain black</a> <a href="http://www.calebcraw.com/air-jordan-super-fly-v-svart-gul-scarpec">air jordan super fly v svart gul</a> <a href="http://www.liquidputter.com/adidas-samba-primeknit-s%C3%B8lv-brun-kurpesc">adidas samba primeknit s酶lv brun</a>
    langbenlang http://www.langbenlang.com/

  2. <a href="http://www.daddeaze.com/womens-nike-huarache-utility-black-denmark-shoeso">womens nike huarache utility black denmark</a> <a href="http://www.debracousins.com/air-jordan-6-midnight-marine-blanc-kurpesb">air jordan 6 midnight marine blanc</a> <a href="http://www.yankaripark.com/air-more-zapatost">air more</a> <a href="http://www.yicailottery.com/girls-high-converse-shoesz">girls high converse</a> <a href="http://www.calebcraw.com/nike-flyknit-air-max-sort-hvid-turbo-gr%C3%B8n-volt-mod-schuheb">nike flyknit air max sort hvid turbo gr酶n volt mod</a> <a href="http://www.liquidputter.com/phil-taylor-washington-redskins-womens-limited-burgundy-number-alternate-80th-anniversary-nike-jersey-red-gold-nflu">phil taylor washington redskins womens limited burgundy number alternate 80th anniversary nike jersey red gold</a>
    dinerwaresb http://www.dinerwaresb.com/

  3. <a href="http://www.helpforaspen.com/nike-air-max-2019-green-uk-shoeso">nike air max 2019 green uk</a> <a href="http://www.mcmartial.com/adidas-neo-high-tops-grau-boots-kurpesc">adidas neo high tops grau boots</a> <a href="http://www.puurderleven.com/cheap-converse-shoes-wholesale-zapatost">cheap converse shoes wholesale</a> <a href="http://www.ipartybooths.com/converse-femme-all-star-blanche-shoesz">converse femme all star blanche</a> <a href="http://www.senegalreview.com/air-jordan-retro-28-minzgr%C3%BCn-schuheb">air jordan retro 28 minzgr眉n</a> <a href="http://www.poshandpearls.com/nike-womens-levine-toilolo-limited-whitepink-jersey-detroit-lions-nfl-87-rush-fashion-nflu">nike womens levine toilolo limited whitepink jersey detroit lions nfl 87 rush fashion</a>
    ductambag http://www.ductambag.com/

  4. <a href="http://www.aleacarolin.com/air-jordan-11-university-r%C3%B8d-velvet-butya">air jordan 11 university r酶d velvet</a> <a href="http://www.atuttogossip.com/air-jordan-2017-basketbtutti-scarpe-highlights-obuvc">air jordan 2017 basketbtutti scarpe highlights</a> <a href="http://www.emssal.com/roshe-run-blu-custom-scarpea">roshe run blu custom</a> <a href="http://www.hobihobinomi.com/asics-gel-quantum-360-in-esecuzione-scarpa-replacemaschiot-scarpee">asics gel quantum 360 in esecuzione scarpa replacemaschiot</a> <a href="http://www.nataliericcio.com/kyrie-3-university-rouge-noir-team-rouge-sox-obuvc">kyrie 3 university rouge noir team rouge sox</a> <a href="http://www.nohitteralert.com/nike-free-run-4.0-v3-blanc-gris-scarpea">nike free run 4.0 v3 blanc gris</a>
    ksmartcity http://www.ksmartcity.com/

  5. <a href="http://www.moldedeps.com/nike-free-5.0-sko-kvinners-gr%C3%A5-sportsb">nike free 5.0 sko kvinners gr氓</a> <a href="http://www.hollerrealtor.com/asics-gel-noosa-tri-7-mujeres-oro-footc">asics gel noosa tri 7 mujeres oro</a> <a href="http://www.nathansills.com/nike-cortez-green-black-sneakerc">nike cortez green black</a> <a href="http://www.walkerhut.com/mackage-kay-coat-for-sale-mackager">mackage kay coat for sale</a> <a href="http://www.freemmark.com/adidas-harden-1-13-below-zero-skor">adidas harden 1 13 below zero</a> <a href="http://www.sncforu.com/detroit-pistons-baseball-jersey-nflr">detroit pistons baseball jersey</a>
    floornhome http://www.floornhome.com/

  6. avatar lauraundleon

    <a href="http://www.divyangmart.com/chicago-bulls-michael-jordan-hoodie-new-adult-size-medium-other-nflv">chicago bulls michael jordan hoodie new adult size medium other</a> <a href="http://www.ezellhvac.com/nike-kobe-10-elite-white-yellow-skof">nike kobe 10 elite white yellow</a> <a href="http://www.oldoyopark.com/nike-5.0-uk-womens-skob">nike 5.0 uk womens</a> <a href="http://www.racquetlady.com/san-diego-padres-winter-hats-online-hata">san diego padres winter hats online</a> <a href="http://www.ankaralokanta.com/nike-free-run-5.0-flyknit-womens-quilt-runningf">nike free run 5.0 flyknit womens quilt</a> <a href="http://www.mengenmetal.com/jordan-retro-1-bl%C3%A5-and-orange-schuhea">jordan retro 1 bl氓 and orange</a>
    lauraundleon http://www.lauraundleon.com/

  7. avatar ozoessaouira

    <a href="http://www.ictbel.com/air-jordan-28-white-bamboo-xr-sneakere">air jordan 28 white bamboo xr</a> <a href="http://www.ioioland.com/air-jordan-jumpman-team-marrone-nero-kurpesa">air jordan jumpman team marrone nero</a> <a href="http://www.sesliesenyurt.com/billig-nike-free-3.0-v3-dam%C3%A4nner-sneakerb">billig nike free 3.0 v3 dam盲nner</a> <a href="http://www.wethinkjerky.com/nike-air-max-tailwind-5-red-in-shoesg">nike air max tailwind 5 red in</a> <a href="http://www.dadees.com/navy-seal-cap-hats">navy seal cap</a> <a href="http://www.pickwickcrc.com/nike-lebron-13-gold-green-sneakerf">nike lebron 13 gold green</a>
    ozoessaouira http://www.ozoessaouira.com/

  8. avatar javajoeshhi

    <a href="http://www.bladeid.com/nike-kyrie-1-dark-gr%C3%A5-pants-scarpef">nike kyrie 1 dark gr氓 pants</a> <a href="http://www.bakomoms.com/air-jordan-13-venta-kit-kurpesa">air jordan 13 venta kit</a> <a href="http://www.macrackmount.com/nike-flyknit-lunar-2-l%C3%B8b-sko-ho14-schuhed">nike flyknit lunar 2 l酶b sko ho14</a> <a href="http://www.martinsedacca.com/nike-free-5.0-grau-blau-kurpesb">nike free 5.0 grau blau</a> <a href="http://www.maxibymaxi.com/air-jordan-14-retro-sort-hvid-kurpesa">air jordan 14 retro sort hvid</a> <a href="http://www.slatervision.com/adidas-porsche-typ-64-noir-orange-obuvc">adidas porsche typ 64 noir orange</a>
    javajoeshhi http://www.javajoeshhi.com/

  9. avatar eworklife

    <a href="http://www.miendiation.com/nike-air-max-2015-ni%C3%B1os-marr%C3%B3n-rojo-scarpec">nike air max 2015 ni帽os marr贸n rojo</a> <a href="http://www.powfarm.com/air-jordan-retro-4-bl%C3%A5-mint-gr%C3%B8nn-kurpesc">air jordan retro 4 bl氓 mint gr酶nn</a> <a href="http://www.nongmobacon.com/ray-ban-tech-liteforce-wayfarer-org-sunglassesf">ray ban tech liteforce wayfarer org</a> <a href="http://www.rentalloka.com/nike-mens-sam-martin-limited-camo-jersey-detroit-lions-nfl-6-rush-realtree-nflr">nike mens sam martin limited camo jersey detroit lions nfl 6 rush realtree</a> <a href="http://www.casamentosweb.com/nike-air-max-95-jacquard-black-queen-skoa">nike air max 95 jacquard black queen</a> <a href="http://www.perheopas.com/blue-mens-nike-air-max-coliseum-racer-sneakerf">blue mens nike air max coliseum racer</a>
    eworklife http://www.eworklife.com/

  10. avatar segwaycr

    <a href="http://www.skynetankara.com/white-hermes-scarf-scarfr">white hermes scarf</a> <a href="http://www.joubertets.com/bmw-nobis-jacket-nobisr">bmw nobis jacket</a> <a href="http://www.mobilsevgili.com/billig-air-jordan-retro-9-dam%C3%A4nner-gold-trainersa">billig air jordan retro 9 dam盲nner gold</a> <a href="http://www.demointegrate.com/cheap-bvlgari-bvlgari-necklace-rose-gold-chain-with-mother-of-pearl-jewelryr">cheap bvlgari bvlgari necklace rose gold chain with mother of pearl</a> <a href="http://www.sekaiblog.com/roshe-run-black-sail-camo-sneakere">roshe run black sail camo</a> <a href="http://www.witeducates.com/north-face-womens-jackets-denali-northfacer">north face womens jackets denali</a>
    segwaycr http://www.segwaycr.com/

  11. avatar globalgtprc

    <a href="http://www.cbchispana.com/cheap-nike-free-women-womens-pink-sportsd">cheap nike free women womens pink</a> <a href="http://www.croquenreve.com/coach-iphone-wallet-wristlet-coachr">coach iphone wallet wristlet</a> <a href="http://www.agnnetworks.com/12-elite-andrew-luck-indianapolis-colts-jersey-new-be-luvd-pink-nflb">12 elite andrew luck indianapolis colts jersey new be luvd pink</a> <a href="http://www.dxmvrc.com/bon-march%C3%A9-nike-free-og-femmes-violet-sportsb">bon march茅 nike free og femmes violet</a> <a href="http://www.dailysewansew.com/nike-flyknit-lunar-1-us-virgin-islands-trainerse">nike flyknit lunar 1 us virgin islands</a> <a href="http://www.prorilasia.com/nike-mercurial-2011-skor">nike mercurial 2011</a>
    globalgtprc http://www.globalgtprc.com/

  12. avatar chieao

    <a href="http://www.kosarkomur.com/limited-keith-rivers-throwback-jersey-dallas-cowboys-56-alternate-white-nflg">limited keith rivers throwback jersey dallas cowboys 56 alternate white</a> <a href="http://www.plakglas.com/nike-air-max-98-release-date-skoa">nike air max 98 release date</a> <a href="http://www.quaderealty.com/jordan-12-powder-blue-carmelo-anthony-runningd">jordan 12 powder blue carmelo anthony</a> <a href="http://www.captmollie.com/nike-free-tr-fit-kvinners-bl%C3%A5-runninge">nike free tr fit kvinners bl氓</a> <a href="http://www.thermilax.com/georgia-boonie-hat-hate">georgia boonie hat</a> <a href="http://www.ladivorcecpa.com/nike-air-force-one-kids-purple-red-trainersg">nike air force one kids purple red</a>
    chieao http://www.chieao.com/

  13. avatar onlyopenhomes

    <a href="http://www.pulselocums.com/chicago-blackhawks-8-bill-mosienko-green-salute-to-service-stitched-nhl-jersey-nflg">chicago blackhawks 8 bill mosienko green salute to service stitched nhl jersey</a> <a href="http://www.topangafire.com/cheap-air-max-97-sale-skoa">cheap air max 97 sale</a> <a href="http://www.eniszczarki.com/blue-pink-womens-nike-dunk-shoes-runningd">blue pink womens nike dunk shoes</a> <a href="http://www.synthcourses.com/nike-air-force-1-flyknit-high-herre-s%C3%B8lv-runninge">nike air force 1 flyknit high herre s酶lv</a> <a href="http://www.chatavalanche.com/purple-sacramento-kings-mitchell-and-ness-nba-solid-snapback-new-era-cap-hate">purple sacramento kings mitchell and ness nba solid snapback new era cap</a> <a href="http://www.katjehempel.com/nike-sb-stefan-janoski-max-hommes-vert-trainersg">nike sb stefan janoski max hommes vert</a>
    onlyopenhomes http://www.onlyopenhomes.com/

  14. avatar jlredcross

    <a href="http://www.nonnagrazia.com/air-max-2015-wmns-nick-trainersa">air max 2015 wmns nick</a> <a href="http://www.warthogrugby.com/kyrie-1-dream-on-foot-shoes1-skoe">kyrie 1 dream on foot shoes1</a> <a href="http://www.uncwliving.com/billig-nike-kobe-10-elite-kvinders-lilla-skoc">billig nike kobe 10 elite kvinders lilla</a> <a href="http://www.halemhomes.com/air-max-2016-orange-lake-shoesc">air max 2016 orange lake</a> <a href="http://www.virgocity.com/bon-march%C3%A9-nike-zoom-pegasus-34-hommes-vert-trainersb">bon march茅 nike zoom pegasus 34 hommes vert</a> <a href="http://www.vspeyedoctors.com/nike-mercurial-victory-v-online-india-skob">nike mercurial victory v online india</a>
    [url=http://www.jlredcross.com/]jlredcross[/url]

  15. avatar allyoucankiss

    <a href="http://www.fintecsales.com/kd-7-gold-white-thunderbolt-skoa">kd 7 gold white thunderbolt</a> <a href="http://www.metinmilli.com/buy-burberry-scarf-scarfr">buy burberry scarf</a> <a href="http://www.cosonventa.com/billig-asics-gel-kinsei-2-menns-svart-sneakerd">billig asics gel kinsei 2 menns svart</a> <a href="http://www.lebosmartlipo.com/billig-nike-blazer-low-dam%C3%A4nner-wei%C3%9F-shoesf">billig nike blazer low dam盲nner wei脽</a> <a href="http://www.talentbschool.com/ray-ban-wayfarer-new-style-sunglassesr">ray ban wayfarer new style</a> <a href="http://www.demuslimah.com/ralph-lauren-mint-green-polo-shirt-2017-polog">ralph lauren mint green polo shirt 2017</a>
    allyoucankiss http://www.allyoucankiss.com/

  16. avatar 美女图片站

    专业内容,无从下口。
    周末了,过来看看,问个好。(。・`ω´・) 欢迎回访。(勿忘我)。

欢迎留言




用户登录

sitemap