4

Right now, I'm trying to determine a method to measure the time that a particular function will take (something like pthread_create). Now, of course, these types of functions are extremely optimized to take as little time as possible; so little, in fact, that my timer that uses gettimeofday in userspace which measures in microseconds is unable to adequately measure anything.

Normally, if I could mess with the kernel, I'd use something like get_cycles to measure the raw number of cycles as a performance metric. However, I haven't found a way to do this in userspace. Is there a way to use get_cycles (or an equivalent) or some other higher precision timer I could use in userspace to measure extremely fast functions?

wibarr
  • 276
  • 1
  • 3
  • 13
  • Which cpu/system/platform? gettimeofday() is your best bet for portable high accuracy time. Bear in mind that a system-wide timer is usually on 'slow' side of the bus, and you might see a fraction of microsecond taken up just accessing the register. Per-core timers will be nearly free to access and high accuracy, but beware the effects of the OS e.g clock speed changes and scheduling between cores. – John Ripley Mar 02 '11 at 08:47
  • You have no chance of getting an accurate timing from user space unless your function call takes (relatively speaking) a long time to execute. Any OS timer method is going to require at least two system calls which themselves take time. Also, if your process you are timing gets switched out of the CPU, you'll be measuring the time other irrelevant processes take to execute. – JeremyP Mar 02 '11 at 09:07

4 Answers4

7

Use RDTSC (if you're on x86), or clock_gettime

unsigned long long cycleCount() {
  asm ("rdtsc");
}
Erik
  • 88,732
  • 13
  • 198
  • 189
  • 2
    rdtsc is a bad idea on recent (last 5 years) systems. The CPU clock speed can change at random (power saving), halt completely if a core is gated, and will tend to drift. I believe Linux makes an effort to synchronize between cores somewhat, but you can't rely on this. Although... if you only want to measure a microsecond of activity and you can repeat/discard outliers, it might be of some use. – John Ripley Mar 02 '11 at 08:43
  • 2
    I've found rdtsc to be quite reliable for short intervals like the OP's talking about, and completely useless over time. – Erik Mar 02 '11 at 08:45
4

clock_gettime allows you to get a nanosecond-precise time from the thread start, process start or epoch.

Delan Azabani
  • 79,602
  • 28
  • 170
  • 210
2

Have you tried getting the time it takes to execute your function, say, 10000 times and taking the mean? That would save the bother of finding more accurate timing functions.

Having said that, this answer: Is gettimeofday() guaranteed to be of microsecond resolution? seems to indicate better functions to use than gettimeofday().

Community
  • 1
  • 1
Matt Ellen
  • 11,268
  • 4
  • 68
  • 90
  • +1 for suggesting to take an average of execution time. While it won't work for somethings, it's pretty helpful in computing things like FPS and such. :) – Miguel Jun 19 '12 at 04:14
1

My linux man page tells me

CONFORMING TO

SVr4, 4.3BSD. POSIX.1-2001 describes gettimeofday() but not settimeofday(). POSIX.1-2008 marks gettimeofday() as obsolete, recomending the use of clock_gettime(2) instead.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177