2

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?

14
  • 2
    Any reason to not to just use a profiler tool like gprof?
    – FatalError
    Mar 25, 2013 at 19:44
  • 3
    "POSIX.1-2008 marks gettimeofday() as obsolete, recommending the use of clock_gettime(2) instead." Mar 25, 2013 at 19:46
  • only profiling short portions of parallel code... you have to patch gprof for profiling threads. perf would be better, but you cannot time individual portions of code. Mar 25, 2013 at 19:46
  • @DanielFischer: Obsolete meaning it doesn't work? What does "obsolete" mean? Mar 25, 2013 at 19:47
  • 4
    RDTSC is slightly harder to use because it's not standard and you need to figure out how many "ticks" there are per unit of time.
    – Mysticial
    Mar 25, 2013 at 19:47

2 Answers 2

3

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.

1
  • A minor note: clock_gettime() supports different clocks to adapt to both use cases (see eg CLOCK_MONOTONIC)
    – loreb
    Mar 25, 2013 at 20:19
3

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.

3
  • 1
    yes. 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 to clock_gettime() with CLOCK_MONOTONIC? Mar 25, 2013 at 20:28
  • No, gettimeofday corresponds to CLOCK_REALTIME. That's another reason to avoid gettimeofday; it's subject to be wrong for measuring intervals if the clock is reset. clock_gettime with CLOCK_MONOTONIC works reliably even if the clock is reset. You can also opt for CLOCK_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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.