[Lemon-commits] [lemon_svn] alpar: r2314 - hugo/trunk/lemon

Lemon SVN svn at lemon.cs.elte.hu
Mon Nov 6 20:51:44 CET 2006


Author: alpar
Date: Tue Nov  8 11:12:45 2005
New Revision: 2314

Modified:
   hugo/trunk/lemon/time_measure.h

Log:
- Timer can be stop()ed and (re)start()ed.
- Obsolete \bug removed


Modified: hugo/trunk/lemon/time_measure.h
==============================================================================
--- hugo/trunk/lemon/time_measure.h	(original)
+++ hugo/trunk/lemon/time_measure.h	Tue Nov  8 11:12:45 2005
@@ -66,6 +66,9 @@
     rtms &getTms() {return ts;}
     const rtms &getTms() const {return ts;}
 
+    void _reset() 
+    { ts.tms_utime=ts.tms_stime=ts.tms_cutime=ts.tms_cstime=0; real_time=0;}
+
   public:
 
     ///Read the current time values of the process
@@ -80,10 +83,13 @@
   
     /// Constructor initializing with zero
     TimeStamp()
-    { ts.tms_utime=ts.tms_stime=ts.tms_cutime=ts.tms_cstime=0; real_time=0;}
+    { _reset(); }
     ///Constructor initializing with the current time values of the process
     TimeStamp(void *) { stamp();}
   
+    ///Set every time value to zero
+    TimeStamp &reset() {_reset();return *this;}
+
     ///\e
     TimeStamp &operator+=(const TimeStamp &b)
     {
@@ -117,8 +123,6 @@
       return t-=b;
     }
     ///\e
-
-    ///\bug operator * and / gives rounded values!
     TimeStamp &operator*=(double b)
     {
       ts.tms_utime*=b;
@@ -180,7 +184,7 @@
     {
       return double(ts.tms_cstime)/sysconf(_SC_CLK_TCK);
     }
-    ///Gives back the real time of the process
+    ///Gives back the real time
     double realTime() const {return real_time;}
   };
 
@@ -189,9 +193,9 @@
     return t*b;
   }
   
-  ///Class measuring the cpu time and real time usage of the process
+  ///Class for measuring the cpu time and real time usage of the process
 
-  ///Class measuring the cpu time and real time usage of the process.
+  ///Class for measuring the cpu time and real time usage of the process.
   ///It is quite easy-to-use, here is a short example.
   ///\code
   ///#include<lemon/time_measure.h>
@@ -214,29 +218,48 @@
   ///}
   ///\endcode
   ///
+  ///The \ref Timer can also be \ref stop() "stopped" and
+  ///\ref start() "started" again, so it is easy to compute collected
+  ///running times.
+  ///
+  ///\warning Depending on the operation system and its actual configuration
+  ///the time counters have a certain (relatively big) granularity.
+  ///Therefore this tool is not appropriate to measure very short times.
+  ///Also, if you start and stop the timer very frequently, it could lead
+  ///distorted results.
+  ///
+  ///The \ref Timer also counts the number of \ref start()
+  ///executions, and is stops only after the same amount (or more)
+  ///\ref stop() "stop()"s. This can be useful e.g. to compute the running time
+  ///of recursive functions.
+  ///
   ///\todo This shouldn't be Unix (Linux) specific.
   ///
   ///\author Alpar Juttner
   class Timer
   {
-    TimeStamp start_time;
-
-    void _reset() {start_time.stamp();}
+    int running; //Timer is running iff running>0; (running>=0 always holds)
+    TimeStamp start_time; //This is the relativ start-time if the timer
+                          //is running, the collected running time otherwise.
+    
+    void _reset() {if(running) start_time.stamp(); else start_time.reset();}
   
   public: 
-    ///Constructor. It starts with zero time counters
-    Timer() {_reset();}
+    ///Constructor.
+
+    ///\param _running indicates whether or not the timer starts immediately.
+    ///
+    Timer(bool _running=true) :running(_running) {_reset();}
 
     ///Computes the ellapsed time
 
     ///This conversion computes the ellapsed time
-    ///since the construction of \c t or since
-    ///the last \c t.reset().
+    ///
     operator TimeStamp () const
     {
       TimeStamp t;
       t.stamp();
-      return t-start_time;
+      return running?t-start_time:start_time;
     }
 
     ///Resets the time counters
@@ -248,7 +271,37 @@
       _reset();
     }
 
+    ///Start the time counters
+    
+    ///This function starts the time counters.
+    ///
+    ///If the timer is started more than ones, it will remain running
+    ///until the same amount of \ref stop() is called.
+    ///\sa stop()
+    void start() 
+    {
+      if(running) running++;
+      else {
+	TimeStamp t;
+	t.stamp();
+	start_time=t-start_time;
+      }
+    }
+    
+    ///Stop the time counters
 
+    ///This function stops the time counters.
+    ///
+    ///\sa stop()
+    void stop() 
+    {
+      if(running && !--running) {
+	TimeStamp t;
+	t.stamp();
+	start_time=t-start_time;
+      }
+    }
+    
     ///Gives back the ellapsed user time of the process
     double userTime() const
     {
@@ -269,7 +322,7 @@
     {
       return operator TimeStamp().cSystemTime();
     }
-    ///Gives back the ellapsed real time of the process
+    ///Gives back the ellapsed real time
     double realTime() const
     {
       return operator TimeStamp().realTime();
@@ -306,7 +359,7 @@
   
   ///This function calls \c f several times and returns the average
   ///running time. The number of the executions will be choosen in such a way
-  ///that the full running time will be roughly between \c min_time
+  ///that the full real running time will be roughly between \c min_time
   ///and <tt>2*min_time</tt>.
   ///\param f the function object to be measured.
   ///\param min_time the minimum total running time.



More information about the Lemon-commits mailing list