lemon/time_measure.h
changeset 1847 7cbc12e42482
parent 1839 b2dfd32b4895
child 1850 50d1d6acfcc2
     1.1 --- a/lemon/time_measure.h	Sat Dec 03 18:30:31 2005 +0000
     1.2 +++ b/lemon/time_measure.h	Mon Dec 05 17:03:31 2005 +0000
     1.3 @@ -17,7 +17,7 @@
     1.4  #ifndef LEMON_TIME_MEASURE_H
     1.5  #define LEMON_TIME_MEASURE_H
     1.6  
     1.7 -///\ingroup misc
     1.8 +///\ingroup timecount
     1.9  ///\file
    1.10  ///\brief Tools for measuring cpu usage
    1.11  
    1.12 @@ -29,7 +29,7 @@
    1.13  
    1.14  namespace lemon {
    1.15  
    1.16 -  /// \addtogroup misc
    1.17 +  /// \addtogroup timecount
    1.18    /// @{
    1.19  
    1.20    /// A class to store (cpu)time instances.
    1.21 @@ -209,7 +209,7 @@
    1.22    ///  Timer T;
    1.23    ///  doSomething();
    1.24    ///  std::cout << T << '\n';
    1.25 -  ///  T.reset();
    1.26 +  ///  T.restart();
    1.27    ///  doSomethingElse();
    1.28    ///  std::cout << T << '\n';
    1.29    ///
    1.30 @@ -223,7 +223,8 @@
    1.31    ///running times.
    1.32    ///
    1.33    ///\warning Depending on the operation system and its actual configuration
    1.34 -  ///the time counters have a certain (relatively big) granularity.
    1.35 +  ///the time counters have a certain (10ms on a typical Linux system)
    1.36 +  ///granularity.
    1.37    ///Therefore this tool is not appropriate to measure very short times.
    1.38    ///Also, if you start and stop the timer very frequently, it could lead
    1.39    ///distorted results.
    1.40 @@ -238,18 +239,18 @@
    1.41    ///\author Alpar Juttner
    1.42    class Timer
    1.43    {
    1.44 -    int running; //Timer is running iff running>0; (running>=0 always holds)
    1.45 +    int _running; //Timer is running iff _running>0; (_running>=0 always holds)
    1.46      TimeStamp start_time; //This is the relativ start-time if the timer
    1.47 -                          //is running, the collected running time otherwise.
    1.48 +                          //is _running, the collected _running time otherwise.
    1.49      
    1.50 -    void _reset() {if(running) start_time.stamp(); else start_time.reset();}
    1.51 +    void _reset() {if(_running) start_time.stamp(); else start_time.reset();}
    1.52    
    1.53    public: 
    1.54      ///Constructor.
    1.55  
    1.56      ///\param _running indicates whether or not the timer starts immediately.
    1.57      ///
    1.58 -    Timer(bool _running=true) :running(_running) {_reset();}
    1.59 +    Timer(bool run=true) :_running(run) {_reset();}
    1.60  
    1.61      ///Computes the ellapsed time
    1.62  
    1.63 @@ -259,15 +260,16 @@
    1.64      {
    1.65        TimeStamp t;
    1.66        t.stamp();
    1.67 -      return running?t-start_time:start_time;
    1.68 +      return _running?t-start_time:start_time;
    1.69      }
    1.70  
    1.71 -    ///Resets the time counters
    1.72 +    ///Reset and stop the time counters
    1.73  
    1.74 -    ///Resets the time counters
    1.75 -    ///
    1.76 +    ///This function resets and stops the time counters
    1.77 +    ///\sa restart()
    1.78      void reset()
    1.79      {
    1.80 +      _running=0;
    1.81        _reset();
    1.82      }
    1.83  
    1.84 @@ -280,27 +282,74 @@
    1.85      ///\sa stop()
    1.86      void start() 
    1.87      {
    1.88 -      if(running) running++;
    1.89 +      if(_running) _running++;
    1.90        else {
    1.91  	TimeStamp t;
    1.92  	t.stamp();
    1.93  	start_time=t-start_time;
    1.94        }
    1.95      }
    1.96 +
    1.97      
    1.98      ///Stop the time counters
    1.99  
   1.100 -    ///This function stops the time counters.
   1.101 -    ///
   1.102 -    ///\sa stop()
   1.103 +    ///This function stops the time counters. If start() was executed more than
   1.104 +    ///once, then the same number of stop() execution is necessary the really
   1.105 +    ///stop the timer.
   1.106 +    /// 
   1.107 +    ///\sa halt()
   1.108 +    ///\sa start()
   1.109 +    ///\sa restart()
   1.110 +    ///\sa reset()
   1.111 +
   1.112      void stop() 
   1.113      {
   1.114 -      if(running && !--running) {
   1.115 +      if(_running && !--_running) {
   1.116  	TimeStamp t;
   1.117  	t.stamp();
   1.118  	start_time=t-start_time;
   1.119        }
   1.120      }
   1.121 +
   1.122 +    ///Halt (i.e stop immediately) the time counters
   1.123 +
   1.124 +    ///This function stops immediately the time counters.
   1.125 +    ///
   1.126 +    ///\sa stop()
   1.127 +    ///\sa restart()
   1.128 +    ///\sa reset()
   1.129 +
   1.130 +    void halt() 
   1.131 +    {
   1.132 +      if(_running) {
   1.133 +	_running=0;
   1.134 +	TimeStamp t;
   1.135 +	t.stamp();
   1.136 +	start_time=t-start_time;
   1.137 +      }
   1.138 +    }
   1.139 +
   1.140 +    ///Returns the running state of the timer
   1.141 +
   1.142 +    ///This function returns the number of stop() exections that is
   1.143 +    ///necessary to really stop the timer.
   1.144 +    ///For example the timer
   1.145 +    ///is running if and only if the return value is \c true
   1.146 +    ///(i.e. greater than
   1.147 +    ///zero).
   1.148 +    int running()  { return _running; }
   1.149 +    
   1.150 +    
   1.151 +    ///Restart the time counters
   1.152 +
   1.153 +    ///This function is a shorthand for
   1.154 +    ///a reset() and a start() calls.
   1.155 +    ///
   1.156 +    void restart() 
   1.157 +    {
   1.158 +      reset();
   1.159 +      start();
   1.160 +    }
   1.161      
   1.162      ///Gives back the ellapsed user time of the process
   1.163      double userTime() const
   1.164 @@ -330,6 +379,25 @@
   1.165  
   1.166    };
   1.167  
   1.168 +  ///Same as \ref Timer but prints a report on destruction.
   1.169 +
   1.170 +  ///Same as \ref Timer but prints a report on destruction.
   1.171 +  ///\todo Untested
   1.172 +  class TimeReport : public Timer 
   1.173 +  {
   1.174 +    std::string _title;
   1.175 +    std::ostream &_os;
   1.176 +  public:
   1.177 +    ///\e
   1.178 +    
   1.179 +    TimeReport(std::string title,std::ostream &os,bool run) 
   1.180 +      : Timer(run), _title(title), _os(os){}
   1.181 +    ~TimeReport() 
   1.182 +    {
   1.183 +      _os << _title << this << std::endl;
   1.184 +    }
   1.185 +  };
   1.186 +      
   1.187    ///Prints the time counters
   1.188  
   1.189    ///Prints the time counters in the following form: