Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The problem is that there are several different time functions available in C and C++, and some of them vary in behavior between implementations. There are also a lot of half-answers floating around. Compiling a list of clock functions together with their properties would answer the question properly. For starts let's ask what the relevant properties are that we're looking for. Looking at your post, I suggest:</p> <ul> <li>What time is measured by the clock? (real, user, system, or, hopefully not, wall-clock?)</li> <li>What is the precision of the clock? (s, ms, µs, or faster?)</li> <li>After how much time does the clock wrap around? Or is there some mechanism to avoid this?</li> <li>Is the clock monotonic, or will it change with changes in the system time (via NTP, time zone, daylight savings time, by the user, etc.)?</li> <li>How do the above vary between implementations?</li> <li>Is the specific function obsolete, non standard, etc.?</li> </ul> <p>Before starting the list, I'd like to point out that wall-clock time is rarely the right time to use, whereas it changes with time zone changes, daylight savings time changes, or if the wall clock is synchronized by NTP. None of these things are good if you're using the time to schedule events or to benchmark performance. It's only really good for what the name says, a clock on the wall (or desktop).</p> <p>Here's what I've found so far for clocks in Linux and OS X:</p> <ul> <li><a href="http://linux.die.net/man/2/time" rel="noreferrer"><code>time()</code></a> returns the wall-clock time from the OS, with precision in seconds.</li> <li><a href="http://linux.die.net/man/3/clock" rel="noreferrer"><code>clock()</code></a> seems to return the sum of user and system time. It is present in C89 and later. At one time this was supposed to be the CPU time in cycles, but modern standards <a href="http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/time.h.html" rel="noreferrer">like POSIX</a> require CLOCKS_PER_SEC to be 1000000, giving a maximum possible precision of 1 µs. The precision on my system is indeed 1 µs. This clock wraps around once it tops out (this typically happens after ~2^32 ticks, which is not very long for a 1 MHz clock). <code>man clock</code> says that since glibc 2.18 it is implemented with <code>clock_gettime(CLOCK_PROCESS_CPUTIME_ID, ...)</code> in Linux. </li> <li><a href="http://linux.die.net/man/3/clock_gettime" rel="noreferrer"><code>clock_gettime(CLOCK_MONOTONIC, ...)</code></a> provides nanosecond resolution, is monotonic. I believe the 'seconds' and 'nanoseconds' are stored separately, each in 32-bit counters. Thus, any wrap-around would occur after many dozen years of uptime. This looks like a very good clock, but unfortunately it isn't yet available on OS X. POSIX 7 <a href="http://pubs.opengroup.org/onlinepubs/9699919799/functions/clock_getres.html" rel="noreferrer">describes <code>CLOCK_MONOTONIC</code> as an optional extension</a>.</li> <li><a href="http://linux.die.net/man/2/getrusage" rel="noreferrer"><code>getrusage()</code></a> turned out to be the best choice for my situation. It reports the user and system times separately and does not wrap around. The precision on my system is 1 µs, but I also tested it on a Linux system (Red Hat 4.1.2-48 with GCC 4.1.2) and there the precision was only 1 ms.</li> <li><a href="http://linux.die.net/man/2/gettimeofday" rel="noreferrer"><code>gettimeofday()</code></a> returns the wall-clock time with (nominally) µs precision. On my system this clock does seem to have µs precision, but this is not guaranteed, because <a href="http://linux.die.net/man/2/gettimeofday" rel="noreferrer">"the resolution of the system clock is hardware dependent"</a>. POSIX.1-2008 <a href="http://pubs.opengroup.org/onlinepubs/9699919799/functions/gettimeofday.html" rel="noreferrer">says that</a>. "Applications should use the <code>clock_gettime()</code> function instead of the obsolescent <code>gettimeofday()</code> function", so you should stay away from it. Linux x86 and implements it <a href="https://github.com/torvalds/linux/blob/v4.4/arch/x86/entry/syscalls/syscall_64.tbl#L105" rel="noreferrer">as a system call</a>.</li> <li><a href="https://developer.apple.com/library/mac/#qa/qa1398/_index.html" rel="noreferrer"><code>mach_absolute_time()</code></a> is an option for very high resolution (ns) timing on OS X. On my system, this does indeed give ns resolution. In principle this clock wraps around, however it is storing ns using a 64-bit unsigned integer, so the wrapping around shouldn't be an issue in practice. Portability is questionable.</li> <li><a href="https://stackoverflow.com/questions/21665641/ns-precision-monotonic-clock-in-c-on-linux-and-os-x/21665642#21665642">I wrote a hybrid function</a> based on <a href="https://gist.github.com/jbenet/1087739" rel="noreferrer">this snippet</a> that uses clock_gettime when compiled on Linux, or a Mach timer when compiled on OS X, in order to get ns precision on both Linux and OS X.</li> </ul> <p>All of the above exist in both Linux and OS X except where otherwise specified. "My system" in the above is an Apple running OS X 10.8.3 with GCC 4.7.2 from MacPorts.</p> <p>Finally, here is a list of references that I found helpful in addition to the links above:</p> <ul> <li><a href="http://blog.habets.pp.se/2010/09/gettimeofday-should-never-be-used-to-measure-time" rel="noreferrer">http://blog.habets.pp.se/2010/09/gettimeofday-should-never-be-used-to-measure-time</a></li> <li><a href="https://stackoverflow.com/questions/7215764/how-to-measure-the-actual-execution-time-of-a-c-program-under-linux">How to measure the ACTUAL execution time of a C program under Linux?</a></li> <li><a href="http://digitalsandwich.com/archives/27-benchmarking-misconceptions-microtime-vs-getrusage.html" rel="noreferrer">http://digitalsandwich.com/archives/27-benchmarking-misconceptions-microtime-vs-getrusage.html</a></li> <li><a href="http://www.unix.com/hp-ux/38937-getrusage.html" rel="noreferrer">http://www.unix.com/hp-ux/38937-getrusage.html</a></li> </ul> <hr> <p><strong>Update</strong>: for OS X, <code>clock_gettime</code> has been implemented as of 10.12 (Sierra). Also, both POSIX and BSD based platforms (like OS X) share the <code>rusage.ru_utime</code> struct field.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload