Microsecond accurate (or better) process timing in Linux

后端 未结 8 2420
醉话见心
醉话见心 2020-12-14 04:01

I need a very accurate way to time parts of my program. I could use the regular high-resolution clock for this, but that will return wallclock time, which is not what I need

相关标签:
8条回答
  • try the CPU's timestamp counter? Wikipedia seems to suggest using clock_gettime().

    0 讨论(0)
  • 2020-12-14 05:01

    If you need very small time units to for (I assume) testing the speed of your software, I would reccomend just running the parts you want to time in a loop millions of times, take the time before and after the loop and calculate the average. A nice side-effect of doing this (apart from not needing to figure out how to use nanoseconds) is that you would get more consistent results because the random overhead caused by the os sceduler will be averaged out.

    Of course, unless your program doesn't need to be able to run millions of times in a second, it's probably fast enough if you can't measure a millisecond running time.

    0 讨论(0)
提交回复
热议问题