- Timer can be stop()ed and (re)start()ed.
authoralpar
Tue, 08 Nov 2005 10:12:45 +0000
changeset 17809f052750753f
parent 1779 f6cafba4dbf2
child 1781 dca4c8a54e0a
- Timer can be stop()ed and (re)start()ed.
- Obsolete \bug removed
lemon/time_measure.h
     1.1 --- a/lemon/time_measure.h	Tue Nov 08 10:10:09 2005 +0000
     1.2 +++ b/lemon/time_measure.h	Tue Nov 08 10:12:45 2005 +0000
     1.3 @@ -66,6 +66,9 @@
     1.4      rtms &getTms() {return ts;}
     1.5      const rtms &getTms() const {return ts;}
     1.6  
     1.7 +    void _reset() 
     1.8 +    { ts.tms_utime=ts.tms_stime=ts.tms_cutime=ts.tms_cstime=0; real_time=0;}
     1.9 +
    1.10    public:
    1.11  
    1.12      ///Read the current time values of the process
    1.13 @@ -80,10 +83,13 @@
    1.14    
    1.15      /// Constructor initializing with zero
    1.16      TimeStamp()
    1.17 -    { ts.tms_utime=ts.tms_stime=ts.tms_cutime=ts.tms_cstime=0; real_time=0;}
    1.18 +    { _reset(); }
    1.19      ///Constructor initializing with the current time values of the process
    1.20      TimeStamp(void *) { stamp();}
    1.21    
    1.22 +    ///Set every time value to zero
    1.23 +    TimeStamp &reset() {_reset();return *this;}
    1.24 +
    1.25      ///\e
    1.26      TimeStamp &operator+=(const TimeStamp &b)
    1.27      {
    1.28 @@ -117,8 +123,6 @@
    1.29        return t-=b;
    1.30      }
    1.31      ///\e
    1.32 -
    1.33 -    ///\bug operator * and / gives rounded values!
    1.34      TimeStamp &operator*=(double b)
    1.35      {
    1.36        ts.tms_utime*=b;
    1.37 @@ -180,7 +184,7 @@
    1.38      {
    1.39        return double(ts.tms_cstime)/sysconf(_SC_CLK_TCK);
    1.40      }
    1.41 -    ///Gives back the real time of the process
    1.42 +    ///Gives back the real time
    1.43      double realTime() const {return real_time;}
    1.44    };
    1.45  
    1.46 @@ -189,9 +193,9 @@
    1.47      return t*b;
    1.48    }
    1.49    
    1.50 -  ///Class measuring the cpu time and real time usage of the process
    1.51 +  ///Class for measuring the cpu time and real time usage of the process
    1.52  
    1.53 -  ///Class measuring the cpu time and real time usage of the process.
    1.54 +  ///Class for measuring the cpu time and real time usage of the process.
    1.55    ///It is quite easy-to-use, here is a short example.
    1.56    ///\code
    1.57    ///#include<lemon/time_measure.h>
    1.58 @@ -214,29 +218,48 @@
    1.59    ///}
    1.60    ///\endcode
    1.61    ///
    1.62 +  ///The \ref Timer can also be \ref stop() "stopped" and
    1.63 +  ///\ref start() "started" again, so it is easy to compute collected
    1.64 +  ///running times.
    1.65 +  ///
    1.66 +  ///\warning Depending on the operation system and its actual configuration
    1.67 +  ///the time counters have a certain (relatively big) granularity.
    1.68 +  ///Therefore this tool is not appropriate to measure very short times.
    1.69 +  ///Also, if you start and stop the timer very frequently, it could lead
    1.70 +  ///distorted results.
    1.71 +  ///
    1.72 +  ///The \ref Timer also counts the number of \ref start()
    1.73 +  ///executions, and is stops only after the same amount (or more)
    1.74 +  ///\ref stop() "stop()"s. This can be useful e.g. to compute the running time
    1.75 +  ///of recursive functions.
    1.76 +  ///
    1.77    ///\todo This shouldn't be Unix (Linux) specific.
    1.78    ///
    1.79    ///\author Alpar Juttner
    1.80    class Timer
    1.81    {
    1.82 -    TimeStamp start_time;
    1.83 -
    1.84 -    void _reset() {start_time.stamp();}
    1.85 +    int running; //Timer is running iff running>0; (running>=0 always holds)
    1.86 +    TimeStamp start_time; //This is the relativ start-time if the timer
    1.87 +                          //is running, the collected running time otherwise.
    1.88 +    
    1.89 +    void _reset() {if(running) start_time.stamp(); else start_time.reset();}
    1.90    
    1.91    public: 
    1.92 -    ///Constructor. It starts with zero time counters
    1.93 -    Timer() {_reset();}
    1.94 +    ///Constructor.
    1.95 +
    1.96 +    ///\param _running indicates whether or not the timer starts immediately.
    1.97 +    ///
    1.98 +    Timer(bool _running=true) :running(_running) {_reset();}
    1.99  
   1.100      ///Computes the ellapsed time
   1.101  
   1.102      ///This conversion computes the ellapsed time
   1.103 -    ///since the construction of \c t or since
   1.104 -    ///the last \c t.reset().
   1.105 +    ///
   1.106      operator TimeStamp () const
   1.107      {
   1.108        TimeStamp t;
   1.109        t.stamp();
   1.110 -      return t-start_time;
   1.111 +      return running?t-start_time:start_time;
   1.112      }
   1.113  
   1.114      ///Resets the time counters
   1.115 @@ -248,7 +271,37 @@
   1.116        _reset();
   1.117      }
   1.118  
   1.119 +    ///Start the time counters
   1.120 +    
   1.121 +    ///This function starts the time counters.
   1.122 +    ///
   1.123 +    ///If the timer is started more than ones, it will remain running
   1.124 +    ///until the same amount of \ref stop() is called.
   1.125 +    ///\sa stop()
   1.126 +    void start() 
   1.127 +    {
   1.128 +      if(running) running++;
   1.129 +      else {
   1.130 +	TimeStamp t;
   1.131 +	t.stamp();
   1.132 +	start_time=t-start_time;
   1.133 +      }
   1.134 +    }
   1.135 +    
   1.136 +    ///Stop the time counters
   1.137  
   1.138 +    ///This function stops the time counters.
   1.139 +    ///
   1.140 +    ///\sa stop()
   1.141 +    void stop() 
   1.142 +    {
   1.143 +      if(running && !--running) {
   1.144 +	TimeStamp t;
   1.145 +	t.stamp();
   1.146 +	start_time=t-start_time;
   1.147 +      }
   1.148 +    }
   1.149 +    
   1.150      ///Gives back the ellapsed user time of the process
   1.151      double userTime() const
   1.152      {
   1.153 @@ -269,7 +322,7 @@
   1.154      {
   1.155        return operator TimeStamp().cSystemTime();
   1.156      }
   1.157 -    ///Gives back the ellapsed real time of the process
   1.158 +    ///Gives back the ellapsed real time
   1.159      double realTime() const
   1.160      {
   1.161        return operator TimeStamp().realTime();
   1.162 @@ -306,7 +359,7 @@
   1.163    
   1.164    ///This function calls \c f several times and returns the average
   1.165    ///running time. The number of the executions will be choosen in such a way
   1.166 -  ///that the full running time will be roughly between \c min_time
   1.167 +  ///that the full real running time will be roughly between \c min_time
   1.168    ///and <tt>2*min_time</tt>.
   1.169    ///\param f the function object to be measured.
   1.170    ///\param min_time the minimum total running time.