lemon/time_measure.h
changeset 124 ae7785fe8431
parent 119 82a2639a05bb
child 126 e1dd2a70737c
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/lemon/time_measure.h	Sat Apr 12 20:38:51 2008 +0100
     1.3 @@ -0,0 +1,541 @@
     1.4 +/* -*- C++ -*-
     1.5 + *
     1.6 + * This file is a part of LEMON, a generic C++ optimization library
     1.7 + *
     1.8 + * Copyright (C) 2003-2008
     1.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
    1.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
    1.11 + *
    1.12 + * Permission to use, modify and distribute this software is granted
    1.13 + * provided that this copyright notice appears in all copies. For
    1.14 + * precise terms see the accompanying LICENSE file.
    1.15 + *
    1.16 + * This software is provided "AS IS" with no warranty of any kind,
    1.17 + * express or implied, and with no claim as to its suitability for any
    1.18 + * purpose.
    1.19 + *
    1.20 + */
    1.21 +
    1.22 +#ifndef LEMON_TIME_MEASURE_H
    1.23 +#define LEMON_TIME_MEASURE_H
    1.24 +
    1.25 +///\ingroup timecount
    1.26 +///\file
    1.27 +///\brief Tools for measuring cpu usage
    1.28 +
    1.29 +#include <sys/times.h>
    1.30 +
    1.31 +#include <sys/time.h>
    1.32 +#include <fstream>
    1.33 +#include <iostream>
    1.34 +#include <unistd.h>
    1.35 +
    1.36 +namespace lemon {
    1.37 +
    1.38 +  /// \addtogroup timecount
    1.39 +  /// @{
    1.40 +
    1.41 +  /// A class to store (cpu)time instances.
    1.42 +
    1.43 +  /// This class stores five time values.
    1.44 +  /// - a real time
    1.45 +  /// - a user cpu time
    1.46 +  /// - a system cpu time
    1.47 +  /// - a user cpu time of children
    1.48 +  /// - a system cpu time of children
    1.49 +  ///
    1.50 +  /// TimeStamp's can be added to or substracted from each other and
    1.51 +  /// they can be pushed to a stream.
    1.52 +  ///
    1.53 +  /// In most cases, perhaps the \ref Timer or the \ref TimeReport
    1.54 +  /// class is what you want to use instead.
    1.55 +  ///
    1.56 +  ///\author Alpar Juttner
    1.57 +
    1.58 +  class TimeStamp
    1.59 +  {
    1.60 +    struct rtms 
    1.61 +    {
    1.62 +      double tms_utime;
    1.63 +      double tms_stime;
    1.64 +      double tms_cutime;
    1.65 +      double tms_cstime;
    1.66 +      rtms() {}
    1.67 +      rtms(tms ts) : tms_utime(ts.tms_utime), tms_stime(ts.tms_stime),
    1.68 +		     tms_cutime(ts.tms_cutime), tms_cstime(ts.tms_cstime) {}
    1.69 +    };
    1.70 +    rtms ts;
    1.71 +    double real_time;
    1.72 +  
    1.73 +    rtms &getTms() {return ts;}
    1.74 +    const rtms &getTms() const {return ts;}
    1.75 +
    1.76 +    void _reset() { 
    1.77 +      ts.tms_utime = ts.tms_stime = ts.tms_cutime = ts.tms_cstime = 0; 
    1.78 +      real_time = 0;
    1.79 +    }
    1.80 +
    1.81 +  public:
    1.82 +
    1.83 +    ///Read the current time values of the process
    1.84 +    void stamp()
    1.85 +    {
    1.86 +      timeval tv;
    1.87 +      tms _ts;
    1.88 +      times(&_ts);
    1.89 +      gettimeofday(&tv, 0);real_time=tv.tv_sec+double(tv.tv_usec)/1e6;
    1.90 +      ts=_ts;
    1.91 +    }
    1.92 +  
    1.93 +    /// Constructor initializing with zero
    1.94 +    TimeStamp()
    1.95 +    { _reset(); }
    1.96 +    ///Constructor initializing with the current time values of the process
    1.97 +    TimeStamp(void *) { stamp();}
    1.98 +  
    1.99 +    ///Set every time value to zero
   1.100 +    TimeStamp &reset() {_reset();return *this;}
   1.101 +
   1.102 +    ///\e
   1.103 +    TimeStamp &operator+=(const TimeStamp &b)
   1.104 +    {
   1.105 +      ts.tms_utime+=b.ts.tms_utime;
   1.106 +      ts.tms_stime+=b.ts.tms_stime;
   1.107 +      ts.tms_cutime+=b.ts.tms_cutime;
   1.108 +      ts.tms_cstime+=b.ts.tms_cstime;
   1.109 +      real_time+=b.real_time;
   1.110 +      return *this;
   1.111 +    }
   1.112 +    ///\e
   1.113 +    TimeStamp operator+(const TimeStamp &b) const
   1.114 +    {
   1.115 +      TimeStamp t(*this);
   1.116 +      return t+=b;
   1.117 +    }
   1.118 +    ///\e
   1.119 +    TimeStamp &operator-=(const TimeStamp &b)
   1.120 +    {
   1.121 +      ts.tms_utime-=b.ts.tms_utime;
   1.122 +      ts.tms_stime-=b.ts.tms_stime;
   1.123 +      ts.tms_cutime-=b.ts.tms_cutime;
   1.124 +      ts.tms_cstime-=b.ts.tms_cstime;
   1.125 +      real_time-=b.real_time;
   1.126 +      return *this;
   1.127 +    }
   1.128 +    ///\e
   1.129 +    TimeStamp operator-(const TimeStamp &b) const
   1.130 +    {
   1.131 +      TimeStamp t(*this);
   1.132 +      return t-=b;
   1.133 +    }
   1.134 +    ///\e
   1.135 +    TimeStamp &operator*=(double b)
   1.136 +    {
   1.137 +      ts.tms_utime*=b;
   1.138 +      ts.tms_stime*=b;
   1.139 +      ts.tms_cutime*=b;
   1.140 +      ts.tms_cstime*=b;
   1.141 +      real_time*=b;
   1.142 +      return *this;
   1.143 +    }
   1.144 +    ///\e
   1.145 +    TimeStamp operator*(double b) const
   1.146 +    {
   1.147 +      TimeStamp t(*this);
   1.148 +      return t*=b;
   1.149 +    }
   1.150 +    friend TimeStamp operator*(double b,const TimeStamp &t);
   1.151 +    ///\e
   1.152 +    TimeStamp &operator/=(double b)
   1.153 +    {
   1.154 +      ts.tms_utime/=b;
   1.155 +      ts.tms_stime/=b;
   1.156 +      ts.tms_cutime/=b;
   1.157 +      ts.tms_cstime/=b;
   1.158 +      real_time/=b;
   1.159 +      return *this;
   1.160 +    }
   1.161 +    ///\e
   1.162 +    TimeStamp operator/(double b) const
   1.163 +    {
   1.164 +      TimeStamp t(*this);
   1.165 +      return t/=b;
   1.166 +    }
   1.167 +    ///The time ellapsed since the last call of stamp()
   1.168 +    TimeStamp ellapsed() const
   1.169 +    {
   1.170 +      TimeStamp t(NULL);
   1.171 +      return t-*this;
   1.172 +    }
   1.173 +  
   1.174 +    friend std::ostream& operator<<(std::ostream& os,const TimeStamp &t);
   1.175 +  
   1.176 +    ///Gives back the user time of the process
   1.177 +    double userTime() const
   1.178 +    {
   1.179 +      return double(ts.tms_utime)/sysconf(_SC_CLK_TCK);
   1.180 +    }
   1.181 +    ///Gives back the system time of the process
   1.182 +    double systemTime() const
   1.183 +    {
   1.184 +      return double(ts.tms_stime)/sysconf(_SC_CLK_TCK);
   1.185 +    }
   1.186 +    ///Gives back the user time of the process' children
   1.187 +    double cUserTime() const
   1.188 +    {
   1.189 +      return double(ts.tms_cutime)/sysconf(_SC_CLK_TCK);
   1.190 +    }
   1.191 +    ///Gives back the user time of the process' children
   1.192 +    double cSystemTime() const
   1.193 +    {
   1.194 +      return double(ts.tms_cstime)/sysconf(_SC_CLK_TCK);
   1.195 +    }
   1.196 +    ///Gives back the real time
   1.197 +    double realTime() const {return real_time;}
   1.198 +  };
   1.199 +
   1.200 +  TimeStamp operator*(double b,const TimeStamp &t) 
   1.201 +  {
   1.202 +    return t*b;
   1.203 +  }
   1.204 +  
   1.205 +  ///Prints the time counters
   1.206 +
   1.207 +  ///Prints the time counters in the following form:
   1.208 +  ///
   1.209 +  /// <tt>u: XX.XXs s: XX.XXs cu: XX.XXs cs: XX.XXs real: XX.XXs</tt>
   1.210 +  ///
   1.211 +  /// where the values are the
   1.212 +  /// \li \c u: user cpu time,
   1.213 +  /// \li \c s: system cpu time,
   1.214 +  /// \li \c cu: user cpu time of children,
   1.215 +  /// \li \c cs: system cpu time of children,
   1.216 +  /// \li \c real: real time.
   1.217 +  /// \relates TimeStamp
   1.218 +  inline std::ostream& operator<<(std::ostream& os,const TimeStamp &t)
   1.219 +  {
   1.220 +    long cls = sysconf(_SC_CLK_TCK);
   1.221 +    os << "u: " << double(t.getTms().tms_utime)/cls <<
   1.222 +      "s, s: " << double(t.getTms().tms_stime)/cls <<
   1.223 +      "s, cu: " << double(t.getTms().tms_cutime)/cls <<
   1.224 +      "s, cs: " << double(t.getTms().tms_cstime)/cls <<
   1.225 +      "s, real: " << t.realTime() << "s";
   1.226 +    return os;
   1.227 +  }
   1.228 +
   1.229 +  ///Class for measuring the cpu time and real time usage of the process
   1.230 +
   1.231 +  ///Class for measuring the cpu time and real time usage of the process.
   1.232 +  ///It is quite easy-to-use, here is a short example.
   1.233 +  ///\code
   1.234 +  /// #include<lemon/time_measure.h>
   1.235 +  /// #include<iostream>
   1.236 +  ///
   1.237 +  /// int main()
   1.238 +  /// {
   1.239 +  ///
   1.240 +  ///   ...
   1.241 +  ///
   1.242 +  ///   Timer t;
   1.243 +  ///   doSomething();
   1.244 +  ///   std::cout << t << '\n';
   1.245 +  ///   t.restart();
   1.246 +  ///   doSomethingElse();
   1.247 +  ///   std::cout << t << '\n';
   1.248 +  ///
   1.249 +  ///   ...
   1.250 +  ///
   1.251 +  /// }
   1.252 +  ///\endcode
   1.253 +  ///
   1.254 +  ///The \ref Timer can also be \ref stop() "stopped" and
   1.255 +  ///\ref start() "started" again, so it is possible to compute collected
   1.256 +  ///running times.
   1.257 +  ///
   1.258 +  ///\warning Depending on the operation system and its actual configuration
   1.259 +  ///the time counters have a certain (10ms on a typical Linux system)
   1.260 +  ///granularity.
   1.261 +  ///Therefore this tool is not appropriate to measure very short times.
   1.262 +  ///Also, if you start and stop the timer very frequently, it could lead to
   1.263 +  ///distorted results.
   1.264 +  ///
   1.265 +  ///\note If you want to measure the running time of the execution of a certain
   1.266 +  ///function, consider the usage of \ref TimeReport instead.
   1.267 +  ///
   1.268 +  ///\todo This shouldn't be Unix (Linux) specific.
   1.269 +  ///\sa TimeReport
   1.270 +  ///
   1.271 +  ///\author Alpar Juttner
   1.272 +  class Timer
   1.273 +  {
   1.274 +    int _running; //Timer is running iff _running>0; (_running>=0 always holds)
   1.275 +    TimeStamp start_time; //This is the relativ start-time if the timer
   1.276 +                          //is _running, the collected _running time otherwise.
   1.277 +    
   1.278 +    void _reset() {if(_running) start_time.stamp(); else start_time.reset();}
   1.279 +  
   1.280 +  public: 
   1.281 +    ///Constructor.
   1.282 +
   1.283 +    ///\param run indicates whether or not the timer starts immediately.
   1.284 +    ///
   1.285 +    Timer(bool run=true) :_running(run) {_reset();}
   1.286 +
   1.287 +    ///\name Control the state of the timer
   1.288 +    ///Basically a Timer can be either running or stopped,
   1.289 +    ///but it provides a bit finer control on the execution.
   1.290 +    ///The \ref Timer also counts the number of \ref start()
   1.291 +    ///executions, and is stops only after the same amount (or more)
   1.292 +    ///\ref stop() "stop()"s. This can be useful e.g. to compute the running time
   1.293 +    ///of recursive functions.
   1.294 +    ///
   1.295 +
   1.296 +    ///@{
   1.297 +
   1.298 +    ///Reset and stop the time counters
   1.299 +
   1.300 +    ///This function resets and stops the time counters
   1.301 +    ///\sa restart()
   1.302 +    void reset()
   1.303 +    {
   1.304 +      _running=0;
   1.305 +      _reset();
   1.306 +    }
   1.307 +
   1.308 +    ///Start the time counters
   1.309 +    
   1.310 +    ///This function starts the time counters.
   1.311 +    ///
   1.312 +    ///If the timer is started more than ones, it will remain running
   1.313 +    ///until the same amount of \ref stop() is called.
   1.314 +    ///\sa stop()
   1.315 +    void start() 
   1.316 +    {
   1.317 +      if(_running) _running++;
   1.318 +      else {
   1.319 +	_running=1;
   1.320 +	TimeStamp t;
   1.321 +	t.stamp();
   1.322 +	start_time=t-start_time;
   1.323 +      }
   1.324 +    }
   1.325 +
   1.326 +    
   1.327 +    ///Stop the time counters
   1.328 +
   1.329 +    ///This function stops the time counters. If start() was executed more than
   1.330 +    ///once, then the same number of stop() execution is necessary the really
   1.331 +    ///stop the timer.
   1.332 +    /// 
   1.333 +    ///\sa halt()
   1.334 +    ///\sa start()
   1.335 +    ///\sa restart()
   1.336 +    ///\sa reset()
   1.337 +
   1.338 +    void stop() 
   1.339 +    {
   1.340 +      if(_running && !--_running) {
   1.341 +	TimeStamp t;
   1.342 +	t.stamp();
   1.343 +	start_time=t-start_time;
   1.344 +      }
   1.345 +    }
   1.346 +
   1.347 +    ///Halt (i.e stop immediately) the time counters
   1.348 +
   1.349 +    ///This function stops immediately the time counters, i.e. <tt>t.halt()</tt>
   1.350 +    ///is a faster
   1.351 +    ///equivalent of the following.
   1.352 +    ///\code
   1.353 +    ///  while(t.running()) t.stop()
   1.354 +    ///\endcode
   1.355 +    ///
   1.356 +    ///
   1.357 +    ///\sa stop()
   1.358 +    ///\sa restart()
   1.359 +    ///\sa reset()
   1.360 +
   1.361 +    void halt() 
   1.362 +    {
   1.363 +      if(_running) {
   1.364 +	_running=0;
   1.365 +	TimeStamp t;
   1.366 +	t.stamp();
   1.367 +	start_time=t-start_time;
   1.368 +      }
   1.369 +    }
   1.370 +
   1.371 +    ///Returns the running state of the timer
   1.372 +
   1.373 +    ///This function returns the number of stop() exections that is
   1.374 +    ///necessary to really stop the timer.
   1.375 +    ///For example the timer
   1.376 +    ///is running if and only if the return value is \c true
   1.377 +    ///(i.e. greater than
   1.378 +    ///zero).
   1.379 +    int running()  { return _running; }
   1.380 +    
   1.381 +    
   1.382 +    ///Restart the time counters
   1.383 +
   1.384 +    ///This function is a shorthand for
   1.385 +    ///a reset() and a start() calls.
   1.386 +    ///
   1.387 +    void restart() 
   1.388 +    {
   1.389 +      reset();
   1.390 +      start();
   1.391 +    }
   1.392 +    
   1.393 +    ///@}
   1.394 +
   1.395 +    ///\name Query Functions for the ellapsed time
   1.396 +
   1.397 +    ///@{
   1.398 +
   1.399 +    ///Gives back the ellapsed user time of the process
   1.400 +    double userTime() const
   1.401 +    {
   1.402 +      return operator TimeStamp().userTime();
   1.403 +    }
   1.404 +    ///Gives back the ellapsed system time of the process
   1.405 +    double systemTime() const
   1.406 +    {
   1.407 +      return operator TimeStamp().systemTime();
   1.408 +    }
   1.409 +    ///Gives back the ellapsed user time of the process' children
   1.410 +    double cUserTime() const
   1.411 +    {
   1.412 +      return operator TimeStamp().cUserTime();
   1.413 +    }
   1.414 +    ///Gives back the ellapsed user time of the process' children
   1.415 +    double cSystemTime() const
   1.416 +    {
   1.417 +      return operator TimeStamp().cSystemTime();
   1.418 +    }
   1.419 +    ///Gives back the ellapsed real time
   1.420 +    double realTime() const
   1.421 +    {
   1.422 +      return operator TimeStamp().realTime();
   1.423 +    }
   1.424 +    ///Computes the ellapsed time
   1.425 +
   1.426 +    ///This conversion computes the ellapsed time, therefore you can print
   1.427 +    ///the ellapsed time like this.
   1.428 +    ///\code
   1.429 +    ///  Timer t;
   1.430 +    ///  doSomething();
   1.431 +    ///  std::cout << t << '\n';
   1.432 +    ///\endcode
   1.433 +    operator TimeStamp () const
   1.434 +    {
   1.435 +      TimeStamp t;
   1.436 +      t.stamp();
   1.437 +      return _running?t-start_time:start_time;
   1.438 +    }
   1.439 +
   1.440 +
   1.441 +    ///@}
   1.442 +  };
   1.443 +
   1.444 +  ///Same as \ref Timer but prints a report on destruction.
   1.445 +
   1.446 +  ///Same as \ref Timer but prints a report on destruction.
   1.447 +  ///This example shows its usage.
   1.448 +  ///\code
   1.449 +  ///  void myAlg(ListGraph &g,int n)
   1.450 +  ///  {
   1.451 +  ///    TimeReport tr("Running time of myAlg: ");
   1.452 +  ///    ... //Here comes the algorithm
   1.453 +  ///  }
   1.454 +  ///\endcode
   1.455 +  ///
   1.456 +  ///\sa Timer
   1.457 +  ///\sa NoTimeReport
   1.458 +  ///\todo There is no test case for this
   1.459 +  class TimeReport : public Timer 
   1.460 +  {
   1.461 +    std::string _title;
   1.462 +    std::ostream &_os;
   1.463 +  public:
   1.464 +    ///\e
   1.465 +
   1.466 +    ///\param title This text will be printed before the ellapsed time.
   1.467 +    ///\param os The stream to print the report to.
   1.468 +    ///\param run Sets whether the timer should start immediately.
   1.469 +
   1.470 +    TimeReport(std::string title,std::ostream &os=std::cerr,bool run=true) 
   1.471 +      : Timer(run), _title(title), _os(os){}
   1.472 +    ///\e Prints the ellapsed time on destruction.
   1.473 +    ~TimeReport() 
   1.474 +    {
   1.475 +      _os << _title << *this << std::endl;
   1.476 +    }
   1.477 +  };
   1.478 +      
   1.479 +  ///'Do nothing' version of \ref TimeReport
   1.480 +
   1.481 +  ///\sa TimeReport
   1.482 +  ///
   1.483 +  class NoTimeReport
   1.484 +  {
   1.485 +  public:
   1.486 +    ///\e
   1.487 +    NoTimeReport(std::string,std::ostream &,bool) {}
   1.488 +    ///\e
   1.489 +    NoTimeReport(std::string,std::ostream &) {}
   1.490 +    ///\e
   1.491 +    NoTimeReport(std::string) {}
   1.492 +    ///\e Do nothing.
   1.493 +    ~NoTimeReport() {}
   1.494 +
   1.495 +    operator TimeStamp () const { return TimeStamp(); }
   1.496 +    void reset() {}
   1.497 +    void start() {}
   1.498 +    void stop() {}
   1.499 +    void halt() {} 
   1.500 +    int running() { return 0; }
   1.501 +    void restart() {}
   1.502 +    double userTime() const { return 0; }
   1.503 +    double systemTime() const { return 0; }
   1.504 +    double cUserTime() const { return 0; }
   1.505 +    double cSystemTime() const { return 0; }
   1.506 +    double realTime() const { return 0; }
   1.507 +  };
   1.508 +      
   1.509 +  ///Tool to measure the running time more exactly.
   1.510 +  
   1.511 +  ///This function calls \c f several times and returns the average
   1.512 +  ///running time. The number of the executions will be choosen in such a way
   1.513 +  ///that the full real running time will be roughly between \c min_time
   1.514 +  ///and <tt>2*min_time</tt>.
   1.515 +  ///\param f the function object to be measured.
   1.516 +  ///\param min_time the minimum total running time.
   1.517 +  ///\retval num if it is not \c NULL, then the actual
   1.518 +  ///        number of execution of \c f will be written into <tt>*num</tt>.
   1.519 +  ///\retval full_time if it is not \c NULL, then the actual
   1.520 +  ///        total running time will be written into <tt>*full_time</tt>.
   1.521 +  ///\return The average running time of \c f.
   1.522 +  
   1.523 +  template<class F>
   1.524 +  TimeStamp runningTimeTest(F f,double min_time=10,unsigned int *num = NULL,
   1.525 +                            TimeStamp *full_time=NULL)
   1.526 +  {
   1.527 +    TimeStamp full;
   1.528 +    unsigned int total=0;
   1.529 +    Timer t;
   1.530 +    for(unsigned int tn=1;tn <= 1U<<31 && full.realTime()<=min_time; tn*=2) {
   1.531 +      for(;total<tn;total++) f();
   1.532 +      full=t;
   1.533 +    }
   1.534 +    if(num) *num=total;
   1.535 +    if(full_time) *full_time=full;
   1.536 +    return full/total;
   1.537 +  }
   1.538 +  
   1.539 +  /// @}  
   1.540 +
   1.541 +
   1.542 +} //namespace lemon
   1.543 +
   1.544 +#endif //LEMON_TIME_MEASURE_H