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