Boost logo

Boost :

From: Jeremy Maitin-Shepard (jbms_at_[hidden])
Date: 2004-04-21 09:16:42

Toon Knapen <toon.knapen_at_[hidden]> writes:

> The documentation of boost::timer states that 'The maximum measurable elapsed
> time may be as low as 596.5 hours (or even less) ...'.

> Well on many 32bit platforms this would probably even be about 71 hours.
> Apparantly POSIX requires CLOCKS_PER_SEC to be 10e6, so if clock_t is 32 bit,
> the maximum of ( clock() / CLOCKS_PER_SEC ) is 4294 seconds.

> Because accuracy for clock() is generally about 1 sec, using clock() is not very
> usefull for doing timings of short (in time) events.

In my experience, clock() is significantly more accurate than 1 second.

> Even more, for short events, the boost::timer might also influence the
> timing too much.

I believe that boost::timer is very light weight.


  boost::timer t;

  /* ... */

  double elapsed = t.elapsed();

Should be equivalent (in efficiency) to:

  std::clock_t initial = clock();

  /* ... */

  double elapsed = static_cast<double>(clock() - initial) / CLOCKS_PER_SEC;

> [snip: clock() useful only for longer timings]

> But for long events the clock_t wraps around too fast (as mentioned above). And
> if you're unlucky that you _start_time = clock() is or a bit smaller than
> std::numeric_limits< clock_t >::max() (thus near the wrap-around point),
> directly afterwards the expression 'clock() - start' will result in a negative
> value.

This can be remedied by storing the time internally using a 64-bit
integer, and then providing a method which the user must call once per
wraparound period in order to achieve accurate results.

> So I would like to suggest for boost::timer to use std::time_t and use
> std::time(NULL) instead of clock() and use std::difftime to calculate the
> elapsed time.

clock() serves as a process CPU-time timer, while time() depends on the
system time, and is also affected by changes to the system clock.
(Also, time() provides only second-precision.)

There was talk of an additional wallclock_timer being added, however; to
be useful, such a facility would ideally be based on a more precise time
source than time(), such as gettimeofday. It appears that the date/time
library includes the partially documented (only through doxygen)
facility microsec_clock, which may or may not be functional on non-POSIX

Jeremy Maitin-Shepard

Boost list run by bdawes at, gregod at, cpdaniel at, john at