37

Hi I wanted to use the clock_gettime() function for measuring the performance of my code.

I am unable to understand the difference between the different kinds of clocks used in the function from the man page descriptions. esp

CLOCK_REALTIME,

CLOCK_PROCESS_CPUTIME_ID

CLOCK_THREAD_CPUTIME_ID

Can someone explaing what each of these clocks do?

Foo Bah
  • 25,660
  • 5
  • 55
  • 79
smilingbuddha
  • 14,334
  • 33
  • 112
  • 189

1 Answers1

46

CLOCK_REALTIME reports the actual wall clock time.

CLOCK_MONOTONIC is for measuring relative real time. It advances at the same rate as the actual flow of time but it's not subject to discontinuities from manual or automatic (NTP) adjustments to the system clock.

CLOCK_PROCESS_CPUTIME_ID is for measuring the amount of CPU time consumed by the process.

CLOCK_THREAD_CPUTIME_ID is for measuring the amount of CPU time consumed by the thread. It's supported by modern kernels and glibc since 2.6.12, but on older linux kernels glibc emulates it badly by simply returning the amount of CPU time consumed by the process since the moment the thread was created.

http://man7.org/linux/man-pages/man2/clock_gettime.2.html

Catskul
  • 17,916
  • 15
  • 84
  • 113
R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
  • So should the speed of an implemented algorithm be measured by CLOCK_REALTIME or by CLOCK_PROCESS_CPUTIME_ID? – smilingbuddha Sep 22 '11 at 01:01
  • 8
    It depends on what you want to measure. `CLOCK_MONOTONIC` should be used if you want to measure total elapsed time, including time spent blocked waiting for IO, but it will also include slowdowns caused by other processes getting scheduled while your program is trying to run. `CLOCK_PROCESS_CPUTIME_ID` will only count actual clock cycles spent executing on your process's behalf (in either userspace or kernelspace, I believe), but not any time spent blocked/sleeping. – R.. GitHub STOP HELPING ICE Sep 22 '11 at 01:46
  • 1
    Note that CLOCK_MONOTONIC is subject to discontinuities from system time adjustment in Linux. CLOCK_MONOTONIC_RAW was defined to get around this. – BD at Rivenhill Feb 16 '12 at 05:56
  • 3
    @BDatRivenhill: I believe you're mistaken. `CLOCK_MONOTONIC` is subject to running fast or slow due to smooth clock adjustment (e.g. by ntpd), but it will never be discontinuous. – R.. GitHub STOP HELPING ICE Feb 16 '12 at 06:11
  • 2
    I would tend to think BDat us correct? See which recommends using CLOCK_MONOTONIC_RAW for game loops because of possibilty of CLOCK_MONOTONIC jumping backward. – ScrollerBlaster Feb 28 '12 at 21:29
  • @ScrollerBlaster: I've actually been searching for information on this issue and can't find anything reliable. Certainly the *intent* of `CLOCK_MONOTONIC` on Linux is that it actually be monotonic and only subject to continuous adjustments for clock skew, not discontinuous clock changes, but a number of people claim there are bugs. Do you have any authoritative sources? Should I open a new question on the matter? – R.. GitHub STOP HELPING ICE Feb 28 '12 at 21:42
  • @R I'm afraid I have nothing authoritative yet. I am trolling through many questions. I'm looking for the ideal thing to use in my game loop. If it can adjust based on periodic updates from the server time then that would be bad, no? Perhaps a question specifically for gamers. – ScrollerBlaster Feb 28 '12 at 21:58
  • 1
    I don't think it's at all gamer-specific. If `CLOCK_MONOTONIC` is broken the way some people say it is, then it should absolutely never be used and the kernel should just remap it to match `CLOCK_MONOTONIC_RAW`. This is why I'm skeptical of claims that it's that broken... – R.. GitHub STOP HELPING ICE Feb 28 '12 at 22:12
  • (I added a note to the link from @ScrollerBlaster.) My understanding is that "raw" is the internal clock, which isn't always spot on (like a clock that runs a bit too fast or too slow). The NTP daemon applies an adjustment to make the clock run at exactly the right speed. So if you want to measure according to the actual passage of time, CLOCK_MONOTONIC should be more accurate. OTOH, for a given machine, CLOCK_MONOTONIC_RAW is more likely to be consistent between runs. Consistently inaccurate, but consistent. :-) – fadden Jun 29 '13 at 00:43
  • 1
    According to [this](http://man7.org/linux/man-pages/man2/clock_gettime.2.html), CLOCK_THREAD_CPUTIME_ID is implemented in Linux kernel since 2.6.12 and glibc 2.4+ natively supports it. – Vlad Lifliand May 21 '14 at 19:24