lemon/time_measure.h
author alpar
Mon, 05 Dec 2005 17:03:31 +0000
changeset 1847 7cbc12e42482
parent 1839 b2dfd32b4895
child 1850 50d1d6acfcc2
permissions -rw-r--r--
- Changed and improved Timer interface
- several new member functions
- reset() -> restart() renaming
- TimeReport: a Timer that prints a report on destruction.
- counter.h: a tool to measure the number of streps of algorithms.
- New documentation module for time measuring and counting.
     1 /* -*- C++ -*-
     2  * lemon/time_measure.h - Part of LEMON, a generic C++ optimization library
     3  *
     4  * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     5  * (Egervary Research Group on Combinatorial Optimization, EGRES).
     6  *
     7  * Permission to use, modify and distribute this software is granted
     8  * provided that this copyright notice appears in all copies. For
     9  * precise terms see the accompanying LICENSE file.
    10  *
    11  * This software is provided "AS IS" with no warranty of any kind,
    12  * express or implied, and with no claim as to its suitability for any
    13  * purpose.
    14  *
    15  */
    16 
    17 #ifndef LEMON_TIME_MEASURE_H
    18 #define LEMON_TIME_MEASURE_H
    19 
    20 ///\ingroup timecount
    21 ///\file
    22 ///\brief Tools for measuring cpu usage
    23 
    24 #include <sys/time.h>
    25 #include <sys/times.h>
    26 #include <fstream>
    27 #include <iostream>
    28 #include <unistd.h>
    29 
    30 namespace lemon {
    31 
    32   /// \addtogroup timecount
    33   /// @{
    34 
    35   /// A class to store (cpu)time instances.
    36 
    37   /// This class stores five time values.
    38   /// - a real time
    39   /// - a user cpu time
    40   /// - a system cpu time
    41   /// - a user cpu time of children
    42   /// - a system cpu time of children
    43   ///
    44   /// TimeStamp's can be added to or substracted from each other and
    45   /// they can be pushed to a stream.
    46   ///
    47   /// In most cases, perhaps \ref Timer class is what you want to use instead.
    48   ///
    49   ///\author Alpar Juttner
    50 
    51   class TimeStamp
    52   {
    53     struct rtms 
    54     {
    55       double tms_utime;
    56       double tms_stime;
    57       double tms_cutime;
    58       double tms_cstime;
    59       rtms() {}
    60       rtms(tms ts) : tms_utime(ts.tms_utime), tms_stime(ts.tms_stime),
    61 		     tms_cutime(ts.tms_cutime), tms_cstime(ts.tms_cstime) {}
    62     };
    63     rtms ts;
    64     double real_time;
    65   
    66     rtms &getTms() {return ts;}
    67     const rtms &getTms() const {return ts;}
    68 
    69     void _reset() 
    70     { ts.tms_utime=ts.tms_stime=ts.tms_cutime=ts.tms_cstime=0; real_time=0;}
    71 
    72   public:
    73 
    74     ///Read the current time values of the process
    75     void stamp()
    76     {
    77       timeval tv;
    78       tms _ts;
    79       times(&_ts);
    80       gettimeofday(&tv, 0);real_time=tv.tv_sec+double(tv.tv_usec)/1e6;
    81       ts=_ts;
    82     }
    83   
    84     /// Constructor initializing with zero
    85     TimeStamp()
    86     { _reset(); }
    87     ///Constructor initializing with the current time values of the process
    88     TimeStamp(void *) { stamp();}
    89   
    90     ///Set every time value to zero
    91     TimeStamp &reset() {_reset();return *this;}
    92 
    93     ///\e
    94     TimeStamp &operator+=(const TimeStamp &b)
    95     {
    96       ts.tms_utime+=b.ts.tms_utime;
    97       ts.tms_stime+=b.ts.tms_stime;
    98       ts.tms_cutime+=b.ts.tms_cutime;
    99       ts.tms_cstime+=b.ts.tms_cstime;
   100       real_time+=b.real_time;
   101       return *this;
   102     }
   103     ///\e
   104     TimeStamp operator+(const TimeStamp &b) const
   105     {
   106       TimeStamp t(*this);
   107       return t+=b;
   108     }
   109     ///\e
   110     TimeStamp &operator-=(const TimeStamp &b)
   111     {
   112       ts.tms_utime-=b.ts.tms_utime;
   113       ts.tms_stime-=b.ts.tms_stime;
   114       ts.tms_cutime-=b.ts.tms_cutime;
   115       ts.tms_cstime-=b.ts.tms_cstime;
   116       real_time-=b.real_time;
   117       return *this;
   118     }
   119     ///\e
   120     TimeStamp operator-(const TimeStamp &b) const
   121     {
   122       TimeStamp t(*this);
   123       return t-=b;
   124     }
   125     ///\e
   126     TimeStamp &operator*=(double b)
   127     {
   128       ts.tms_utime*=b;
   129       ts.tms_stime*=b;
   130       ts.tms_cutime*=b;
   131       ts.tms_cstime*=b;
   132       real_time*=b;
   133       return *this;
   134     }
   135     ///\e
   136     TimeStamp operator*(double b) const
   137     {
   138       TimeStamp t(*this);
   139       return t*=b;
   140     }
   141     friend TimeStamp operator*(double b,const TimeStamp &t);
   142     ///\e
   143     TimeStamp &operator/=(double b)
   144     {
   145       ts.tms_utime/=b;
   146       ts.tms_stime/=b;
   147       ts.tms_cutime/=b;
   148       ts.tms_cstime/=b;
   149       real_time/=b;
   150       return *this;
   151     }
   152     ///\e
   153     TimeStamp operator/(double b) const
   154     {
   155       TimeStamp t(*this);
   156       return t/=b;
   157     }
   158     ///The time ellapsed since the last call of stamp()
   159     TimeStamp ellapsed() const
   160     {
   161       TimeStamp t(NULL);
   162       return t-*this;
   163     }
   164   
   165     friend std::ostream& operator<<(std::ostream& os,const TimeStamp &t);
   166   
   167     ///Gives back the user time of the process
   168     double userTime() const
   169     {
   170       return double(ts.tms_utime)/sysconf(_SC_CLK_TCK);
   171     }
   172     ///Gives back the system time of the process
   173     double systemTime() const
   174     {
   175       return double(ts.tms_stime)/sysconf(_SC_CLK_TCK);
   176     }
   177     ///Gives back the user time of the process' children
   178     double cUserTime() const
   179     {
   180       return double(ts.tms_cutime)/sysconf(_SC_CLK_TCK);
   181     }
   182     ///Gives back the user time of the process' children
   183     double cSystemTime() const
   184     {
   185       return double(ts.tms_cstime)/sysconf(_SC_CLK_TCK);
   186     }
   187     ///Gives back the real time
   188     double realTime() const {return real_time;}
   189   };
   190 
   191   TimeStamp operator*(double b,const TimeStamp &t) 
   192   {
   193     return t*b;
   194   }
   195   
   196   ///Class for measuring the cpu time and real time usage of the process
   197 
   198   ///Class for measuring the cpu time and real time usage of the process.
   199   ///It is quite easy-to-use, here is a short example.
   200   ///\code
   201   ///#include<lemon/time_measure.h>
   202   ///#include<iostream>
   203   ///
   204   ///int main()
   205   ///{
   206   ///
   207   ///  ...
   208   ///
   209   ///  Timer T;
   210   ///  doSomething();
   211   ///  std::cout << T << '\n';
   212   ///  T.restart();
   213   ///  doSomethingElse();
   214   ///  std::cout << T << '\n';
   215   ///
   216   ///  ...
   217   ///
   218   ///}
   219   ///\endcode
   220   ///
   221   ///The \ref Timer can also be \ref stop() "stopped" and
   222   ///\ref start() "started" again, so it is possible 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 (10ms on a typical Linux system)
   227   ///granularity.
   228   ///Therefore this tool is not appropriate to measure very short times.
   229   ///Also, if you start and stop the timer very frequently, it could lead
   230   ///distorted results.
   231   ///
   232   ///The \ref Timer also counts the number of \ref start()
   233   ///executions, and is stops only after the same amount (or more)
   234   ///\ref stop() "stop()"s. This can be useful e.g. to compute the running time
   235   ///of recursive functions.
   236   ///
   237   ///\todo This shouldn't be Unix (Linux) specific.
   238   ///
   239   ///\author Alpar Juttner
   240   class Timer
   241   {
   242     int _running; //Timer is running iff _running>0; (_running>=0 always holds)
   243     TimeStamp start_time; //This is the relativ start-time if the timer
   244                           //is _running, the collected _running time otherwise.
   245     
   246     void _reset() {if(_running) start_time.stamp(); else start_time.reset();}
   247   
   248   public: 
   249     ///Constructor.
   250 
   251     ///\param _running indicates whether or not the timer starts immediately.
   252     ///
   253     Timer(bool run=true) :_running(run) {_reset();}
   254 
   255     ///Computes the ellapsed time
   256 
   257     ///This conversion computes the ellapsed time
   258     ///
   259     operator TimeStamp () const
   260     {
   261       TimeStamp t;
   262       t.stamp();
   263       return _running?t-start_time:start_time;
   264     }
   265 
   266     ///Reset and stop the time counters
   267 
   268     ///This function resets and stops the time counters
   269     ///\sa restart()
   270     void reset()
   271     {
   272       _running=0;
   273       _reset();
   274     }
   275 
   276     ///Start the time counters
   277     
   278     ///This function starts the time counters.
   279     ///
   280     ///If the timer is started more than ones, it will remain running
   281     ///until the same amount of \ref stop() is called.
   282     ///\sa stop()
   283     void start() 
   284     {
   285       if(_running) _running++;
   286       else {
   287 	TimeStamp t;
   288 	t.stamp();
   289 	start_time=t-start_time;
   290       }
   291     }
   292 
   293     
   294     ///Stop the time counters
   295 
   296     ///This function stops the time counters. If start() was executed more than
   297     ///once, then the same number of stop() execution is necessary the really
   298     ///stop the timer.
   299     /// 
   300     ///\sa halt()
   301     ///\sa start()
   302     ///\sa restart()
   303     ///\sa reset()
   304 
   305     void stop() 
   306     {
   307       if(_running && !--_running) {
   308 	TimeStamp t;
   309 	t.stamp();
   310 	start_time=t-start_time;
   311       }
   312     }
   313 
   314     ///Halt (i.e stop immediately) the time counters
   315 
   316     ///This function stops immediately the time counters.
   317     ///
   318     ///\sa stop()
   319     ///\sa restart()
   320     ///\sa reset()
   321 
   322     void halt() 
   323     {
   324       if(_running) {
   325 	_running=0;
   326 	TimeStamp t;
   327 	t.stamp();
   328 	start_time=t-start_time;
   329       }
   330     }
   331 
   332     ///Returns the running state of the timer
   333 
   334     ///This function returns the number of stop() exections that is
   335     ///necessary to really stop the timer.
   336     ///For example the timer
   337     ///is running if and only if the return value is \c true
   338     ///(i.e. greater than
   339     ///zero).
   340     int running()  { return _running; }
   341     
   342     
   343     ///Restart the time counters
   344 
   345     ///This function is a shorthand for
   346     ///a reset() and a start() calls.
   347     ///
   348     void restart() 
   349     {
   350       reset();
   351       start();
   352     }
   353     
   354     ///Gives back the ellapsed user time of the process
   355     double userTime() const
   356     {
   357       return operator TimeStamp().userTime();
   358     }
   359     ///Gives back the ellapsed system time of the process
   360     double systemTime() const
   361     {
   362       return operator TimeStamp().systemTime();
   363     }
   364     ///Gives back the ellapsed user time of the process' children
   365     double cUserTime() const
   366     {
   367       return operator TimeStamp().cUserTime();
   368     }
   369     ///Gives back the ellapsed user time of the process' children
   370     double cSystemTime() const
   371     {
   372       return operator TimeStamp().cSystemTime();
   373     }
   374     ///Gives back the ellapsed real time
   375     double realTime() const
   376     {
   377       return operator TimeStamp().realTime();
   378     }
   379 
   380   };
   381 
   382   ///Same as \ref Timer but prints a report on destruction.
   383 
   384   ///Same as \ref Timer but prints a report on destruction.
   385   ///\todo Untested
   386   class TimeReport : public Timer 
   387   {
   388     std::string _title;
   389     std::ostream &_os;
   390   public:
   391     ///\e
   392     
   393     TimeReport(std::string title,std::ostream &os,bool run) 
   394       : Timer(run), _title(title), _os(os){}
   395     ~TimeReport() 
   396     {
   397       _os << _title << this << std::endl;
   398     }
   399   };
   400       
   401   ///Prints the time counters
   402 
   403   ///Prints the time counters in the following form:
   404   ///
   405   /// <tt>u: XX.XXs s: XX.XXs cu: XX.XXs cs: XX.XXs real: XX.XXs</tt>
   406   ///
   407   /// where the values are the
   408   /// \li \c u: user cpu time,
   409   /// \li \c s: system cpu time,
   410   /// \li \c cu: user cpu time of children,
   411   /// \li \c cs: system cpu time of children,
   412   /// \li \c real: real time.
   413   /// \relates TimeStamp
   414   inline std::ostream& operator<<(std::ostream& os,const TimeStamp &t)
   415   {
   416     long cls = sysconf(_SC_CLK_TCK);
   417     os << "u: " << double(t.getTms().tms_utime)/cls <<
   418       "s, s: " << double(t.getTms().tms_stime)/cls <<
   419       "s, cu: " << double(t.getTms().tms_cutime)/cls <<
   420       "s, cs: " << double(t.getTms().tms_cstime)/cls <<
   421       "s, real: " << t.realTime() << "s";
   422     return os;
   423   }
   424 
   425   
   426   ///Tool to measure the running time more exactly.
   427   
   428   ///This function calls \c f several times and returns the average
   429   ///running time. The number of the executions will be choosen in such a way
   430   ///that the full real running time will be roughly between \c min_time
   431   ///and <tt>2*min_time</tt>.
   432   ///\param f the function object to be measured.
   433   ///\param min_time the minimum total running time.
   434   ///\retval num if it is not \c NULL, then *num will contain the actual
   435   ///        number of execution of \c f.
   436   ///\retval full_time if it is not \c NULL, then *full_time
   437   ///        will contain the actual
   438   ///        total running time.
   439   ///\return The average running time of \c f.
   440   
   441   template<class F>
   442   TimeStamp runningTimeTest(F f,double min_time=10,int *num = NULL,
   443 			TimeStamp *full_time=NULL)
   444   {
   445     Timer t;
   446     TimeStamp full;
   447     int total=0;
   448     for(int tn=1;tn < 1<<24; tn*=2) {
   449       for(;total<tn;total++) f();
   450       full=t;
   451       if(full.realTime()>min_time) {
   452 	if(num) *num=total;
   453 	if(full_time) *full_time=full;
   454       return full/total;
   455       }
   456     }
   457     return TimeStamp();
   458   }
   459   
   460   /// @}  
   461 
   462 
   463 } //namespace lemon
   464 
   465 #endif //LEMON_TIME_MEASURE_H