Re: clock() function
From: Mike Wahler (mkwahler_at_mkwahler.net)
Date: 10/09/03
- Next message: Robert Stankowic: "Re: clock() function"
- Previous message: Richard Bos: "Re: use of long double"
- In reply to: Robert Stankowic: "Re: clock() function"
- Next in thread: Robert Stankowic: "Re: clock() function"
- Reply: Robert Stankowic: "Re: clock() function"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Thu, 09 Oct 2003 08:22:03 GMT
"Robert Stankowic" <pcdoktor@netway.at> wrote in message
news:3f851422$0$16062$91cee783@newsreader02.highway.telekom.at...
>
> "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).
Yes, I reviewed what I wrote after I posted it, and posted
a followup, with an ISO quote about clock(), saying essentially
"wait a minute, still too many unanswered questions,
I'm not sure." :-)
The bit about the -1 return only says "if value not available
or cannot be represented". I think that 'wrapping' from
max value to zero is outside this context, but I also
am not certain.
Maybe someone else will add more input about the standard.
I do think that given what the OP's implementation
documents, for it, an algorithm for "wrap adjustment"
could probably be devised, if enough control can
be exercised over the interval between 'clock()' polls.
Thanks for your input.
-Mike
- Next message: Robert Stankowic: "Re: clock() function"
- Previous message: Richard Bos: "Re: use of long double"
- In reply to: Robert Stankowic: "Re: clock() function"
- Next in thread: Robert Stankowic: "Re: clock() function"
- Reply: Robert Stankowic: "Re: clock() function"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|