2

Is there a platform-independent way to measure time up to micro seconds using C standard library?

Paul R
  • 208,748
  • 37
  • 389
  • 560
Leonid
  • 22,360
  • 25
  • 67
  • 91

5 Answers5

2

The precision of the measurement depends on the operating system, unfortunately.

salezica
  • 74,081
  • 25
  • 105
  • 166
2

While the above answers are certainly correct (c'est la vie), they are not very helpful.

I have done some testing with Cygwin under Windows XP: on my machine, the granularity of gettimeofday() is about 15 msecs (1/64 secs?). Which is big. And so is the granularity of:

  • clock_t clock(void) (divisor CLOCKS_PER_SEC)
  • clock_t times(struct tms *) (divisor sysconf(_SC_CLK_TCK))

Both divisors are 1000 (Does POSIX has 1000000 for first?).

Also, clock_getres(CLOCK_REALTIME,...) returns 15 msecs, so clock_gettime() is unlikely to help. And CLOCK_MONOTONIC and CLOCK_PROCESS_CPUTIME_ID don't work.

Other possibilites for CYGWIN might be RDTSC; see the Wikipedia article. And maybe HPET, but it isn't available with Windows XP.

Also note in Linux, clock() is the process time, while in Windows it is the wall time.

So some sample code, both for standard Unix, and for CYGWIN code running under Windows, which gives a granularity of about 50 microsecs (on my machine):

#if !CYGWIN
double realElapsedTime(void) {              // returns 0 first time called
    static struct timeval t0;
    struct timeval tv;
    gettimeofday(&tv, 0);
    if (!t0.tv_sec)                         // one time initialization
        t0 = tv;
    return tv.tv_sec - t0.tv_sec + (tv.tv_usec - t0.tv_usec) / 1000000.;
}
#else
#include <windows.h>
double realElapsedTime(void) {              // granularity about 50 microsecs
    static LARGE_INTEGER freq, start;
    LARGE_INTEGER count;
    if (!QueryPerformanceCounter(&count))
        assert(0 && "QueryPerformanceCounter");
    if (!freq.QuadPart) {                   // one time initialization
        if (!QueryPerformanceFrequency(&freq))
            assert(0 && "QueryPerformanceFrequency");
        start = count;
    }
    return (double)(count.QuadPart - start.QuadPart) / freq.QuadPart;
}
#endif
Joseph Quinsey
  • 9,553
  • 10
  • 54
  • 77
  • See also grieve's anwer in shttp://stackoverflow.com/questions/275004/c-timer-function-to-provide-time-in-nano-seconds – Joseph Quinsey Dec 06 '10 at 00:23
  • `clock` and `times` return the amount of **cpu time** your program has consumed, not the amount of real time passed. Unless your program is the only process running and burns the cpu at 100% without sleeping or performing any blocking operations, the cpu time and real time will be **radically** different. – R.. GitHub STOP HELPING ICE Dec 06 '10 at 05:32
  • @R: As I noted, MSDN says clock(CRT) gives **elapsed wall-clock-time**. And neonsignal in 2005 in http://www.linuxquestions.org/questions/programming-9/question-about-clock-function-773394/ has verified this. – Joseph Quinsey Dec 07 '10 at 01:08
  • 1
    Excellent answer for Cygwin. – Georgie Jul 25 '17 at 01:28
1

The POSIX gettimeofday and clock_gettime functions are the closest thing you'll find to platform-independence. Ideally all platforms would follow POSIX, but one notable infamous one (along with various obscure ones) doesn't.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
0

I'd use a combination of clock() and CLOCKS_PER_SEC

paquetp
  • 1,639
  • 11
  • 19
  • 2
    Depends what you mean by "time", then, since `clock()` stops if the program is descheduled. – Steve Jessop Dec 05 '10 at 17:26
  • And while it's OS independent, it is still platform dependent since you're depending on the resolution of the system clock. C'est la vie though, nothing you can really do about that. – Bryan Marble Dec 05 '10 at 17:30
  • I just felt the need to describe to people _C'est la vie_ means 'that is life' or 'such is life'. Translated word for word though, it comes out more like 'It is the life'. – Dominic K Dec 05 '10 at 17:48
  • even though it's vendor dependent, you could check the resolution and not support your application for that particular vendor if the resolution is not as required. It's best to use OS specific routines though for this sort of thing. POSIX clock_gettime comes to mind. – paquetp Dec 05 '10 at 18:29
  • @Steve Jessop: Good point, you'd need a real time operating system to mitigate any significant 'jitter' from scheduling. You still may get some (interrupts and such), but probably not so much that the resolution of any implementation of clock() would notice. – paquetp Dec 05 '10 at 18:43
0

time() is standard for practically all C runtime libraries, but has only 1.0 second resolution. In C++ there are the BOOST ptime microsec_clock::universal_time() and ptime microsec_clock::local_time() functions.

wallyk
  • 56,922
  • 16
  • 83
  • 148