Microsecond accurate (or better) process timing in Linux

后端 未结 8 2419
醉话见心
醉话见心 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条回答
  • 2020-12-14 04:42

    If you are looking for this level of timing resolution, you are probably trying to do some micro-optimization. If that's the case, you should look at PAPI. Not only does it provide both wall-clock and virtual (process only) timing information, it also provides access to CPU event counters, which can be indispensable when you are trying to improve performance.

    http://icl.cs.utk.edu/papi/

    0 讨论(0)
  • 2020-12-14 04:44

    You can use the High Precision Event Timer (HPET) if you have a fairly recent 2.6 kernel. Check out Documentation/hpet.txt on how to use it. This solution is platform dependent though and I believe it is only available on newer x86 systems. HPET has at least a 10MHz timer so it should fit your requirements easily.

    I believe several PowerPC implementations from Freescale support a cycle exact instruction counter as well. I used this a number of years ago to profile highly optimized code but I can't remember what it is called. I believe Freescale has a kernel patch you have to apply in order to access it from user space.

    0 讨论(0)
  • 2020-12-14 04:47

    See this question for some more info.

    Something I've used for such things is gettimeofday(). It provides a structure with seconds and microseconds. Call it before the code, and again after. Then just subtract the two structs using timersub, and you can get the time it took in seconds from the tv_usec field.

    0 讨论(0)
  • 2020-12-14 04:50

    I think I found the kernel patch I was looking for. Posting it here so I don't forget the link:

    http://user.it.uu.se/~mikpe/linux/perfctr/ http://sourceforge.net/projects/perfctr/

    Edit: It works for my purposes, though not very user-friendly.

    0 讨论(0)
  • I believe CFC (Completely Fair Scheduler) is what you're looking for.

    0 讨论(0)
  • 2020-12-14 04:59

    http://allmybrain.com/2008/06/10/timing-cc-code-on-linux/

    might be of help to you (directly if you are doing it in C/C++, but I hope it will give you pointers even if you're not)... It claims to provide microsecond accuracy, which just passes your criterion. :)

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