Is clock_gettime() adequate for submicrosecond timing?

后端 未结 6 1495
误落风尘
误落风尘 2020-12-28 16:14

I need a high-resolution timer for the embedded profiler in the Linux build of our application. Our profiler measures scopes as small as individual functions, so it needs a

6条回答
  •  北荒
    北荒 (楼主)
    2020-12-28 16:44

    I ran some benchmarks on my system which is a quad core E5645 Xeon supporting a constant TSC running kernel 3.2.54 and the results were:

    clock_gettime(CLOCK_MONOTONIC_RAW)       100ns/call
    clock_gettime(CLOCK_MONOTONIC)           25ns/call
    clock_gettime(CLOCK_REALTIME)            25ns/call
    clock_gettime(CLOCK_PROCESS_CPUTIME_ID)  400ns/call
    rdtsc (implementation @DavidSchwarz)     600ns/call
    

    So it looks like on a reasonably modern system the (accepted answer) rdtsc is the worst route to go down.

提交回复
热议问题