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