lemon/time_measure.h
changeset 1783 474666e89a2a
parent 1689 f1795dafe42c
child 1806 1530c115580f
equal deleted inserted replaced
1:f6f2cbacd3a0 2:552a171eb2d2
    64     double real_time;
    64     double real_time;
    65   
    65   
    66     rtms &getTms() {return ts;}
    66     rtms &getTms() {return ts;}
    67     const rtms &getTms() const {return ts;}
    67     const rtms &getTms() const {return ts;}
    68 
    68 
       
    69     void _reset() 
       
    70     { ts.tms_utime=ts.tms_stime=ts.tms_cutime=ts.tms_cstime=0; real_time=0;}
       
    71 
    69   public:
    72   public:
    70 
    73 
    71     ///Read the current time values of the process
    74     ///Read the current time values of the process
    72     void stamp()
    75     void stamp()
    73     {
    76     {
    78       ts=_ts;
    81       ts=_ts;
    79     }
    82     }
    80   
    83   
    81     /// Constructor initializing with zero
    84     /// Constructor initializing with zero
    82     TimeStamp()
    85     TimeStamp()
    83     { ts.tms_utime=ts.tms_stime=ts.tms_cutime=ts.tms_cstime=0; real_time=0;}
    86     { _reset(); }
    84     ///Constructor initializing with the current time values of the process
    87     ///Constructor initializing with the current time values of the process
    85     TimeStamp(void *) { stamp();}
    88     TimeStamp(void *) { stamp();}
    86   
    89   
       
    90     ///Set every time value to zero
       
    91     TimeStamp &reset() {_reset();return *this;}
       
    92 
    87     ///\e
    93     ///\e
    88     TimeStamp &operator+=(const TimeStamp &b)
    94     TimeStamp &operator+=(const TimeStamp &b)
    89     {
    95     {
    90       ts.tms_utime+=b.ts.tms_utime;
    96       ts.tms_utime+=b.ts.tms_utime;
    91       ts.tms_stime+=b.ts.tms_stime;
    97       ts.tms_stime+=b.ts.tms_stime;
   115     {
   121     {
   116       TimeStamp t(*this);
   122       TimeStamp t(*this);
   117       return t-=b;
   123       return t-=b;
   118     }
   124     }
   119     ///\e
   125     ///\e
   120 
       
   121     ///\bug operator * and / gives rounded values!
       
   122     TimeStamp &operator*=(double b)
   126     TimeStamp &operator*=(double b)
   123     {
   127     {
   124       ts.tms_utime*=b;
   128       ts.tms_utime*=b;
   125       ts.tms_stime*=b;
   129       ts.tms_stime*=b;
   126       ts.tms_cutime*=b;
   130       ts.tms_cutime*=b;
   178     ///Gives back the user time of the process' children
   182     ///Gives back the user time of the process' children
   179     double cSystemTime() const
   183     double cSystemTime() const
   180     {
   184     {
   181       return double(ts.tms_cstime)/sysconf(_SC_CLK_TCK);
   185       return double(ts.tms_cstime)/sysconf(_SC_CLK_TCK);
   182     }
   186     }
   183     ///Gives back the real time of the process
   187     ///Gives back the real time
   184     double realTime() const {return real_time;}
   188     double realTime() const {return real_time;}
   185   };
   189   };
   186 
   190 
   187   TimeStamp operator*(double b,const TimeStamp &t) 
   191   TimeStamp operator*(double b,const TimeStamp &t) 
   188   {
   192   {
   189     return t*b;
   193     return t*b;
   190   }
   194   }
   191   
   195   
   192   ///Class measuring the cpu time and real time usage of the process
   196   ///Class for measuring the cpu time and real time usage of the process
   193 
   197 
   194   ///Class measuring the cpu time and real time usage of the process.
   198   ///Class for measuring the cpu time and real time usage of the process.
   195   ///It is quite easy-to-use, here is a short example.
   199   ///It is quite easy-to-use, here is a short example.
   196   ///\code
   200   ///\code
   197   ///#include<lemon/time_measure.h>
   201   ///#include<lemon/time_measure.h>
   198   ///#include<iostream>
   202   ///#include<iostream>
   199   ///
   203   ///
   212   ///  ...
   216   ///  ...
   213   ///
   217   ///
   214   ///}
   218   ///}
   215   ///\endcode
   219   ///\endcode
   216   ///
   220   ///
       
   221   ///The \ref Timer can also be \ref stop() "stopped" and
       
   222   ///\ref start() "started" again, so it is easy to compute collected
       
   223   ///running times.
       
   224   ///
       
   225   ///\warning Depending on the operation system and its actual configuration
       
   226   ///the time counters have a certain (relatively big) granularity.
       
   227   ///Therefore this tool is not appropriate to measure very short times.
       
   228   ///Also, if you start and stop the timer very frequently, it could lead
       
   229   ///distorted results.
       
   230   ///
       
   231   ///The \ref Timer also counts the number of \ref start()
       
   232   ///executions, and is stops only after the same amount (or more)
       
   233   ///\ref stop() "stop()"s. This can be useful e.g. to compute the running time
       
   234   ///of recursive functions.
       
   235   ///
   217   ///\todo This shouldn't be Unix (Linux) specific.
   236   ///\todo This shouldn't be Unix (Linux) specific.
   218   ///
   237   ///
   219   ///\author Alpar Juttner
   238   ///\author Alpar Juttner
   220   class Timer
   239   class Timer
   221   {
   240   {
   222     TimeStamp start_time;
   241     int running; //Timer is running iff running>0; (running>=0 always holds)
   223 
   242     TimeStamp start_time; //This is the relativ start-time if the timer
   224     void _reset() {start_time.stamp();}
   243                           //is running, the collected running time otherwise.
       
   244     
       
   245     void _reset() {if(running) start_time.stamp(); else start_time.reset();}
   225   
   246   
   226   public: 
   247   public: 
   227     ///Constructor. It starts with zero time counters
   248     ///Constructor.
   228     Timer() {_reset();}
   249 
       
   250     ///\param _running indicates whether or not the timer starts immediately.
       
   251     ///
       
   252     Timer(bool _running=true) :running(_running) {_reset();}
   229 
   253 
   230     ///Computes the ellapsed time
   254     ///Computes the ellapsed time
   231 
   255 
   232     ///This conversion computes the ellapsed time
   256     ///This conversion computes the ellapsed time
   233     ///since the construction of \c t or since
   257     ///
   234     ///the last \c t.reset().
       
   235     operator TimeStamp () const
   258     operator TimeStamp () const
   236     {
   259     {
   237       TimeStamp t;
   260       TimeStamp t;
   238       t.stamp();
   261       t.stamp();
   239       return t-start_time;
   262       return running?t-start_time:start_time;
   240     }
   263     }
   241 
   264 
   242     ///Resets the time counters
   265     ///Resets the time counters
   243 
   266 
   244     ///Resets the time counters
   267     ///Resets the time counters
   246     void reset()
   269     void reset()
   247     {
   270     {
   248       _reset();
   271       _reset();
   249     }
   272     }
   250 
   273 
   251 
   274     ///Start the time counters
       
   275     
       
   276     ///This function starts the time counters.
       
   277     ///
       
   278     ///If the timer is started more than ones, it will remain running
       
   279     ///until the same amount of \ref stop() is called.
       
   280     ///\sa stop()
       
   281     void start() 
       
   282     {
       
   283       if(running) running++;
       
   284       else {
       
   285 	TimeStamp t;
       
   286 	t.stamp();
       
   287 	start_time=t-start_time;
       
   288       }
       
   289     }
       
   290     
       
   291     ///Stop the time counters
       
   292 
       
   293     ///This function stops the time counters.
       
   294     ///
       
   295     ///\sa stop()
       
   296     void stop() 
       
   297     {
       
   298       if(running && !--running) {
       
   299 	TimeStamp t;
       
   300 	t.stamp();
       
   301 	start_time=t-start_time;
       
   302       }
       
   303     }
       
   304     
   252     ///Gives back the ellapsed user time of the process
   305     ///Gives back the ellapsed user time of the process
   253     double userTime() const
   306     double userTime() const
   254     {
   307     {
   255       return operator TimeStamp().userTime();
   308       return operator TimeStamp().userTime();
   256     }
   309     }
   267     ///Gives back the ellapsed user time of the process' children
   320     ///Gives back the ellapsed user time of the process' children
   268     double cSystemTime() const
   321     double cSystemTime() const
   269     {
   322     {
   270       return operator TimeStamp().cSystemTime();
   323       return operator TimeStamp().cSystemTime();
   271     }
   324     }
   272     ///Gives back the ellapsed real time of the process
   325     ///Gives back the ellapsed real time
   273     double realTime() const
   326     double realTime() const
   274     {
   327     {
   275       return operator TimeStamp().realTime();
   328       return operator TimeStamp().realTime();
   276     }
   329     }
   277 
   330 
   304   
   357   
   305   ///Tool to measure the running time more exactly.
   358   ///Tool to measure the running time more exactly.
   306   
   359   
   307   ///This function calls \c f several times and returns the average
   360   ///This function calls \c f several times and returns the average
   308   ///running time. The number of the executions will be choosen in such a way
   361   ///running time. The number of the executions will be choosen in such a way
   309   ///that the full running time will be roughly between \c min_time
   362   ///that the full real running time will be roughly between \c min_time
   310   ///and <tt>2*min_time</tt>.
   363   ///and <tt>2*min_time</tt>.
   311   ///\param f the function object to be measured.
   364   ///\param f the function object to be measured.
   312   ///\param min_time the minimum total running time.
   365   ///\param min_time the minimum total running time.
   313   ///\retval num if it is not \c NULL, then *num will contain the actual
   366   ///\retval num if it is not \c NULL, then *num will contain the actual
   314   ///        number of execution of \c f.
   367   ///        number of execution of \c f.