Re: clock() function

From: Tim Prince (timothyprince_at_xxxxsbcglobal.net)
Date: 10/09/03


Date: Thu, 09 Oct 2003 12:11:15 GMT

Robert Stankowic wrote:

>
> "Mike Wahler" <mkwahler@mkwahler.net> schrieb im Newsbeitrag
> news:tF7hb.3975$dn6.414@newsread4.news.pas.earthlink.net...
>>
>> "Pushkar Pradhan" <pushkar@gri.msstate.edu> wrote in message
>> news:3F84B960.8040607@gri.msstate.edu...
>> > I'm using clock() to time parts of my code
>> > e.g.
>> > clk1 = clock();
>> > /* code */
>> > clk2 = clock();
>> > /* calculate time in secs */
>> > .....
>> > clk1 = clock();
>> > /* code */
>> > clk2 = clock();
>> > /* calculate time in secs */
>> >
>> > I have to do this a couple of more times. I'm worried after reading the
>> > man page for clock() which says:
>> > USAGE
>> > The value returned by clock() is defined in microseconds for
>> > compatibility with systems that have CPU clocks with much
>> > higher resolution. Because of this, the value returned will
>> > wrap around after accumulating only 2147 seconds of CPU time
>> > (about 36 minutes).
>> >
>> > Does this mean if my code runs more than 36 mins. the timings
>> > calculated will be wrong?
>>
>
> [....]
>
>>
>> But you need not store the value in a type 'clock_t'
>> object. Use a type with a range large enough for
>> our anticipated needs, e.g. type 'double'. The required
>> range of type 'double' is significantly higher than
>> that of a 31 bit integer value.
>>
>> double start = (double)clock();
>> double elapsed = 0;
>>
>> /* etc */
>>
>> elapsed = (double)clock() - start;
>
> "The clock function returns the implementation’s best approximation to the
> processor
> time used by the program since the beginning of an implementation-defined
> era related
> only to the program invocation. To determine the time in seconds, the
> value returned by
> the clock function should be divided by the value of the macro
> CLOCKS_PER_SEC. If
> the processor time used is not available or its value cannot be
> represented, the function
>
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> returns the value (clock_t)-1"
>
> If I am not mistaken, the value _returned_ from clock() will (maybe, see
> below) wrap, so storing that in a double won't help, and the period the OP
> can use may even be much less than these 36 minutes, because the first
> call to clock() will most likely return a value > 0.
> I could not find anything in the standard about what happens if the
> maximum value representable in a clock_t is exceeded so I am not sure
> whether the value will actually wrap on every implementation, but the last
> sentence in the standard's description above suggests to me, that in this
> case the return value may be -1. (or is even _required_ to return -1...
> not sure about that from the wording).
>
> kind regards
> Robert
The usual wrap behavior allows differences to be taken of intervals up to
double the quoted value. For the larger intervals, the difference is
wrapped into negative numbers, not to zero or an error value. Thus, it is
possible to recover a meaningful value for an interval up to an hour by
treating it as unsigned. As the documentation on my system says,
        Note that the time can wrap around. On a 32bit system
       where CLOCKS_PER_SEC equals 1000000 this function will
       return the same value approximately every 72 minutes.
Neither the standard nor the previously quoted documentation provide
assurance of that.
IMHO clock() would be more useful if it returned a 64-bit type. Failing
that, adherence to the posix value of CLOCKS_PER_SEC rather than the
physical resolution appears misguided.

-- 
Tim Prince


Relevant Pages

  • Re: clock() function
    ... > "The clock function returns the implementation’s best approximation to the ... > only to the program invocation. ... > below) wrap, so storing that in a double won't help, and the period the OP ... Maybe someone else will add more input about the standard. ...
    (comp.lang.c)
  • Re: What Is Wrong With Newswatcher?
    ... chars that demands the client to hard wrap the lines either ... don't recall brackets being a standard in the rfc to enclose wrapped ...    A multi-line data block is used in certain commands and responses. ...
    (comp.sys.mac.advocacy)
  • Re: Another Tricky Problem I am Messing With (Not Homework)
    ... just pointed out your and Keith Thompson's ... I don't believe I've misunderstood the C standard, ... The clock function determines the processor time used. ...
    (comp.lang.c)
  • Re: compat_sys_times() bogus until jiffies >= 0.
    ... delays the wrap around much further. ... POSIX only demands consistency ... The man page for linux states that the return code is time since system ... the standard requires. ...
    (Linux-Kernel)
  • Re: clock()
    ... The clock function returns the implementation’s best approximation to the ... meaning is never explained in the standard. ... > related only to the program invocation." ...
    (microsoft.public.vc.language)