4

I'm using a quite simple code to measure for time of execution.It works well until I am not sure may be not more than 20 minutes.But after(>20min.)it is returning negative results.I searched throughout the forums and tried everything like changing the datatype,using long unsigned (which is returning 0) but failed again. The following is the snippet of my code

main()
{
    time_t start,stop;
    double time_arm;
    start = clock(); 
    /* .......  */
    stop = clock();
    time_arm=(double)(stop-start)/(double)CLOCKS_PER_SEC;

    printf("Time Taken by ARM only is %lf \n",time_arm);
}

output is Time Taken by ARM only is -2055.367296

Any help is appreciated,thanks in advance.

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
pylearner
  • 63
  • 1
  • 7

3 Answers3

6

POSIX requires CLOCKS_PER_SEC to be 1,000,000. That means your count is in microseconds - and 231 microseconds is about 35 minutes. Your timer is just overflowing, so you can't get meaningful results when that happens.

Why you see the problem at 20 minutes, I'm not sure - maybe your CLOCKS_PER_SEC isn't POSIX-compatible. Regardless, your problem is timer overflow. You'll need to handle this problem in a different way - maybe look into getrusage(2).

Carl Norum
  • 219,201
  • 40
  • 422
  • 469
  • Note that this is a bug in the implementation. C requires `clock` to fail (returning `(clock_t)-1`) if the result is not representable; wrapping is not permitted. See the interpretation for [Austin Group issue 686](http://austingroupbugs.net/view.php?id=686). – R.. GitHub STOP HELPING ICE Oct 14 '13 at 20:11
2

clock_t is long which is a 32-bit signed value - it can hold 2^31 before it overflows. On a 32-bit system the CLOCKS_PER_SEC value is equal to 1000000 [as mentioned by POSIX] clock() will return the same value almost after every 72 minutes.

According to MSDN also it can return -1.

The elapsed wall-clock time since the start of the process (elapsed time in seconds times CLOCKS_PER_SEC). If the amount of elapsed time is unavailable, the function returns –1, cast as a clock_t.

On a side note:-

clock() measures CPU time used by the program and it does NOT measure real time and clock() return value is specified in microseconds.

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
  • Was looking into the definition of `clock_t`. Found nothing to say it is `long`, only its "range and precision ... are implementation-defined". (C11dr 7.21.1) Do you have some useful reference to show it is 'long' or 32-bit? – chux - Reinstate Monica Oct 14 '13 at 19:45
  • 1
    @chux:- I am not going to bet that it is long but I have found that long is usually used. As for one refernce you can check this:- **In the GNU system, clock_t is equivalent to long int and CLOCKS_PER_SEC is an integer value.** http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_19.html Do correct me if I am wrong? :( – Rahul Tripathi Oct 14 '13 at 19:50
  • @chux:- Is that wrong? Or did I missed something. Please do comment if I missed anything. Thanks! :) – Rahul Tripathi Oct 14 '13 at 20:08
  • Copied that reference: In GNU clock_t is a `long int` Now I wonder if in a GNU system, is a `long int` 32-bit or at least 32-bit. Hmmm. I suspect the latter. – chux - Reinstate Monica Oct 14 '13 at 20:10
  • @chux:- It is 32 bit what I have learnt so far. Do correct me if I am wrong! – Rahul Tripathi Oct 14 '13 at 20:12
  • 1
    [Integer Types](http://www.gnu.org/software/gnu-c-manual/gnu-c-manual.html#Integer-Types) say 32-bit or 64-bit. – chux - Reinstate Monica Oct 14 '13 at 20:17
  • @chux:- Yes thats correct. Got it. Thanks for updating my knowledge. +1 P.S. Do I need to do some corrections in my answer also? – Rahul Tripathi Oct 14 '13 at 20:19
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/39215/discussion-between-chux-and-rahul-tripathi) – chux - Reinstate Monica Oct 14 '13 at 20:28
0

You could try using clock_gettime() with the CLOCK_PROCESS_CPUTIME_ID option if you are on a POSIX system. clock_gettime() uses struct timespec to return time information so doesn't suffer from the same problem as using a single integer.

ralight
  • 11,033
  • 3
  • 49
  • 59