Spencer Killen
Spencer Killen

Reputation: 25

Is clock() reliable for a timer?

I'm using clock(), and I'm wondering whether it ever resets or maxes out. all I'm using it for is just too subject it from a previous function call and find the difference.

Thanks for the help so far but I'm not really able to get the chrono thing working in VS '12 but its fine because I think its a little more than I need anyway, I was think about using 's time() but I have no idea how to convert the t_time into an int that contains just the current seconds 0-60, any help?

Upvotes: 1

Views: 1378

Answers (3)

Carl
Carl

Reputation: 44448

The simple answer is that if you're just using it to time a function, it will probably not wrap around. It may also be too slow and chances are you might see a function duration of zero. If you want accurate timing for a function that executes fast, you're probably better using an OS level call like this one on Windows.

Upvotes: 0

Mats Petersson
Mats Petersson

Reputation: 129364

Depends on what system you are on. It may use a 32+ or a 64-bit clock_t. It will definitely roll over, but if it's 64-bit, it will be OK for quite some time before it rolls over - 264 microseconds is still an awful long time (approx 244 seconds, and there is around 216 seconds per day, so 228 days - which is about 220, or a million, years... ;)

Of course, in a 32-bit system, we have about 212=4096 seconds at microsecond resoltion. An hour being 3600s = about 1h10m.

However, another problem, in some systems, is that clock() returns CPU time used, so if you sleep, it won't count as time in clock().

And of course, even though CLOCKS_PER_SEC may be 1000000, it doesn't mean that you get microsecond resultion - in many systems, it "jumps" 10000 units at a time.

In summary, "probably a bad idea".

If you have C++11 on the system, use std::chrono, which has several options for timekeeping that are sufficiently good for most purposes (but do study the std::chrono docs)

Example code:

#include <iostream>
#include <chrono>
#include <unistd.h>  // replace with "windows.h" if needed.

int main()
{
    std::chrono::time_point<std::chrono::system_clock> start, end;

    start = std::chrono::system_clock::now();
// 10 seconds on a unix system. Sleep(10000) on windows will be the same thing
    sleep(10);   
    end = std::chrono::system_clock::now();

    int elapsed_seconds = std::chrono::duration_cast<std::chrono::seconds>
                             (end-start).count();
    std::cout << "elapsed time: " << elapsed_seconds << "s\n";
}

Upvotes: 1

Matteo Italia
Matteo Italia

Reputation: 126787

As far as the standard is concerned,

The range and precision of times representable in clock_t and time_t are implementation-defined.

(C99, §7.23.1 ¶4)

so there are no guarantees of range; the definition of clock() does not say anything about wrapping around, although it says that

If the processor time used is not available or its value cannot be represented, the function returns the value (clock_t)(-1)

So we may say that exceeding the range of clock_t may be seen as "its value cannot be represented"; on the other hand, this interpretation would mean that, after some time, clock() becomes completely useless.

In facts, if we get down to a specific implementation (glibc), we see:

matteo@teokubuntu:~$ man 3 clock
   Note that  the  time  can  wrap  around.   On  a  32-bit  system  where
   CLOCKS_PER_SEC  equals 1000000 this function will return the same value
   approximately every 72 minutes.

Upvotes: 3

Related Questions