Boost logo

Boost :

From: Jeff Garland (jeff_at_[hidden])
Date: 2003-05-21 01:31:41


> > Here's my simple implementation of new timers, which should work on
> > both linux and windows nt. I hope you'll intested in this idea and
> > give some comments. Thanks.
>
> Let's look more seriously at the Boost Date-Time library first. I don't
> want to make any changes to Boost timer without first understanding the
> pros and cons of basing timers on Date-Time.

The only thing that boost::date_time will give you is a nice interface
for handling the total elapsed time (eg: time_duration). This provides
for better output and integer-based computations instead of floating point.
However, there will still need to be a way to measure the time at the point
the timer is started when it is stopped or measured. For this I want to be
able to swap out various implementations.

Below is what I a sketch of what I have in mind. Pretty similar to
the current timer, but using a traits specification to define
core methods and types. Also, it has a total_elapsed duration
value which means that rollover issues are reduced for clock_t
type implementations since the total_elapsed is snapped when
the timer is stopped.

template <class timer_traits>
class timer
{
public:
  typedef timer_traits traits_type;
  typedef typename timer_traits::time_rep time_rep;
  typedef typename timer_traits::time_duration_type time_duration_type;
  
  timer ();
  bool is_running() const;
  void start();
  time_duration_type elapsed() const;
  void stop();
  void restart();
  void clear_elapsed();
    
private:
  bool running;
  time_rep started_at;
  time_duration_type total_elapsed;
};

With this approach we can create a 'wallclock' timer using one of the clock
classes from the date_time library (see testTimer.cpp for examples)
Now we can do interesting things like:

  timer<wallclock_timer_traits> wall_timer;
  wall_timer.start();
  timer<clockt_timer_traits> clockt_timer;
  clockt_timer.start();
  ...
  //calculate difference between wall and process time.
  time_duration diff = wall_timer - clockt_timer;

The attached files contain the timer class including the implementation, a
preliminary test program that has traits types for both the wallclock timer and
the clock_t timer. Note the wallclock timer is not currently portable to
non-posix systems. Test output from Linux with gcc 3.2. Feedback appreciated.

Jeff





Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk