I'd like to profile some code in C using Linux. I usually just do the usual gettimeofday()
functions, they're easy to put in, understandable, and portable. I've had someone ask me why not using CPU counters or RDTSC for profiling the code, but although I understand that RDTSC may be more precise, I cannot argue in favor or against one or the other. So, my question is: all else being equal, would people profile code with the usual time functions or the RDTSC?
2 Answers
CPU counters and wall clocks are different tools for different purposes.
When to use a wall clock:
When you want to measure time in a standard time unit (such as seconds). If you want to measure how long X task takes, use a wall clock.
Examples:
clock()
gettimeofday()
clock_gettime(2)
- etc...
When to use RDTSC:
If you're looking to measure the relative times of two different tasks to as high precision as possible, then RDTSC may be suitable.
RDTSC measures the number of pseudo-cycles that have elapsed since the CPU has started up. Often (but not always), this is equal to the CPU clock speed of your processor. But there's no easy to determine the exact number of "ticks per second" without actually measuring it against a wall clock.
However, RDTSC is about as low overhead as it can get for a time function. So it is well suited for micro-optimizations when you're comparing one implementation against another to determine which is faster. (as opposed to how much absolute time it takes)
Other things to note:
- In most cases, most benchmarking purposes can be done sufficiently well with wall clocks. So the use of RDTSC is pretty limited. Stick with standardized functions when possible.
- High precision wall clocks are typically implemented on top of RDTSC. So if you're trying to use RDTSC to get a high-precision measurement of wall time, you'll just be reinventing the wheel.
As a side note, I use RDTSC both for seeing RNGs and as an anti-cheating measure for my overclocker benchmarks.
-
A minor note: clock_gettime() supports different clocks to adapt to both use cases (see eg CLOCK_MONOTONIC)– lorebMar 25, 2013 at 20:19
For most purposes, I would use clock_gettime
. As others have said, gettimeofday
is obsolescent and its resolution is too low for measuring times in the sub-10000-cycle range. Note that clock_gettime
may have syscall overhead (kernel entry/exit cost) reflected in the differences you take, but on modern x86_64 Linux systems, clock_gettime
actually runs fully in userspace and uses rdtsc
under the hood, so it ends up just being a much more portable, cleanly abstracted version of rdtsc
.
-
1yes. RDTSC is just another form of "all the world's a VAX" style programming, just of the Intel flavor. Stick with the portable interfaces wherever possible. Mar 25, 2013 at 20:15
-
so, just to make sure,
gettimeofday()
is functionally equivalent toclock_gettime()
withCLOCK_MONOTONIC
? Mar 25, 2013 at 20:28 -
No,
gettimeofday
corresponds toCLOCK_REALTIME
. That's another reason to avoidgettimeofday
; it's subject to be wrong for measuring intervals if the clock is reset.clock_gettime
withCLOCK_MONOTONIC
works reliably even if the clock is reset. You can also opt forCLOCK_PROCESS_CPUTIME_CLOCKID
if you want to just measure the cpu time spent by your process, not including other processes that might preempt it. Mar 25, 2013 at 21:06
gprof
?gettimeofday()
as obsolete, recommending the use ofclock_gettime(2)
instead."