1

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?

  • If you're worried about it, check out librt's clock_gettime(CLOCK_MONOTONIC, ...) instead. That might be more in line with what you want, assuming you are allowed to use the librt library. – Jeremy Friesner Jun 14 '13 at 23:43

3 Answers3

3

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.
Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
1

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";
}
Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
  • thanks, I was having a lot of trouble with chrono though do you think you could give me a code where it just returns the amount of seconds since the last time it was called? – Spencer Killen Jun 14 '13 at 23:12
  • `std::this_thread::sleep_for(std::chrono::seconds(10));` is C++11 equivalent for `sleep` (may be not supported in some compilers) – milleniumbug Jun 14 '13 at 23:28
  • @milleniumbug: Thanks. Couldn't be bothered to try to look it up - I don't use `chrono` very much, so I just used something I knew what it was. I guess I could have used `char dummy; cin >> dummy;` instead... – Mats Petersson Jun 14 '13 at 23:37
  • @MatsPetersson Well, it certainly doesn't matter in this example - just thought I should mention it. – milleniumbug Jun 14 '13 at 23:52
0

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.

Community
  • 1
  • 1
Carl
  • 43,122
  • 10
  • 80
  • 104