[Home]GDTL/Timer

BOOST WIKI | GDTL | RecentChanges | Preferences | Page List | Links List

This page is in response to a request to take the boost::timer functionality, update it, and make it part of the date_time library. Along the way the interface will be redesigned to use the features of date_time like class time_duration for the interface.

Design Goals / Issues

There are a number of goals and issues that will be addressed as part of this timer port including:

These are discussed in more detail in the following sections.

Using Time Duration for Interfaces

Proposed Interface

T.B.D

Interface Additions in Response to James Fowler

 //not compiled, just a detailed sketch
 template<class clock>             
 class timer
 {
 public:
  enum START_OPTION = {AUTOSTART, MANUAL_START};
  timer(time_duration initial_duration = time_duration(0,0,0)
        START_OPTION = AUTOSTART) : 
    start_time_(ptime(not_a_date_time)),
    elapsed_(initial_duration),
    {
       if (START_OPTION == AUTO_START) 
       {
          start();
       }
    } 
  void start() 
  { 
    start_time_ = clock::local_time(); 
  } 
  void restart() 
  { 
    clear_elapsed();
    start();
  } 
  time_duration elapsed() const                  
  { 
    if (start_time_ != ptime(not_a_date_time)) {
      boost::posix_time::ptime current = clock::local_time();
      elapsed_ += (current - start_time_);
      start_time_ = current;
    }
    return elapsed_;
  }
  void pause()                  
  { 
    boost::posix_time::ptime current = clock::local_time();
    elapsed_ += (current - start_time_);
    start_time_ = ptime(not_a_date_time); //not_a_date_time signals a not-running state
  }
  void continue()
  { 
    boost::posix_time::ptime current = clock::local_time();
    elapsed_ += (current - start_time_);
    start_time_ = ptime(not_a_date_time);
  }
  void clear_elapsed()
  {
    elapsed_ = time_duration(0,0,0); 
  }
 private:
  mutable boost::posix_time::ptime start_time_;
  boost::posix_time::time_duration elapsed_;
 }; // timer

Example of Using Date-Time With Current Timer Interfaces

 //just replacing the current timer interfaces with date-time classes like time_duration and posix_time
 class timer
 {
 public:
  timer() : state_time_(boost::posix_time::microsec_clock::local_time()) 
    {} // postcondition: elapsed()==0
  void   restart() { start_time_ = microsec_clock::local_time(); } // post: elapsed()==0
  time_duration elapsed() const                  
    { return  boost::posix_time::microsec_clock::local_time() - start_time_;}

  //this would depend on the clock.  TBD on how to replace this not sure it's needed given time_duration range...
  //double elapsed_max() const   // return estimated maximum value for elapsed()
  // Portability warning: elapsed_max() may return too high a value on systems
  // where std::clock_t overflows or resets at surprising values.
  //{
  //  return (double((std::numeric_limits<std::clock_t>::max)())
  //     - double(_start_time)) / double(CLOCKS_PER_SEC); 
  //}

  //this would depend on the clock.  TBD on how to replace this
  //double elapsed_min() const            // return minimum value for elapsed()
  // { return double(1)/double(CLOCKS_PER_SEC); }

 private:
  boost::posix_time::ptime start_time_;
 }; // timer

Different Clock Types / Resolutions

T.B.D

Mail Threads

Other References


BOOST WIKI | GDTL | RecentChanges | Preferences | Page List | Links List
Edit text of this page | View other revisions
Last edited March 6, 2005 4:09 pm (diff)
Search:
Disclaimer: This site not officially maintained by Boost Developers