subsystems/timer.hpp

    1: #ifndef STLPLUS_TIMER
    2: #define STLPLUS_TIMER
    3: ////////////////////////////////////////////////////////////////////////////////
    4: 
    5: //   Author:    Andy Rushton
    6: //   Copyright: (c) Andy Rushton, 2007
    7: //   License:   BSD License, see ../docs/license.html
    8: 
    9: //   A CPU timer encapsulated as a class. Measures the CPU time used since its
   10: //   construction and allows this cumulative time to be reported at any time.
   11: 
   12: ////////////////////////////////////////////////////////////////////////////////
   13: #include "subsystems_fixes.hpp"
   14: #include <time.h>
   15: #include <string>
   16: #include <iostream>
   17: 
   18: namespace stlplus
   19: {
   20: 
   21:   ////////////////////////////////////////////////////////////////////////////////
   22: 
   23:   class timer
   24:   {
   25:   private:
   26:     clock_t m_clock;
   27:     time_t m_time;
   28: 
   29:   public:
   30:     // constructor resets the timer to zero
   31:     timer(void);
   32:     ~timer(void);
   33: 
   34:     // reset the timer to zero without destroying it
   35:     void reset(void);
   36: 
   37:     // get the elapsed time in seconds, expressed as a float
   38:     float elapsed(void) const;
   39:     // get the CPU time in seconds, expressed as a float
   40:     float cpu(void) const;
   41: 
   42:     // get a printable string representing the elapsed time and CPU time
   43:     std::string text(void) const;
   44:   };
   45: 
   46:   ////////////////////////////////////////////////////////////////////////////////
   47: 
   48:   // print the elapsed time and CPU time using the same representation as the text method
   49:   std::ostream& operator << (std::ostream&, const timer&);
   50: 
   51:   ////////////////////////////////////////////////////////////////////////////////
   52: 
   53: } // end namespace stlplus
   54: 
   55: #endif