lemon/time_measure.h
author hegyi
Tue, 08 Apr 2008 11:39:40 +0000
changeset 2602 1c7790d9e025
parent 2543 a0443c411220
permissions -rw-r--r--
Rel.07 NEWS - 3. round
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@2553
     5
 * Copyright (C) 2003-2008
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@2408
   236
  /// #include<lemon/time_measure.h>
alpar@2408
   237
  /// #include<iostream>
alpar@814
   238
  ///
alpar@2408
   239
  /// int main()
alpar@2408
   240
  /// {
alpar@458
   241
  ///
alpar@2408
   242
  ///   ...
alpar@458
   243
  ///
alpar@2543
   244
  ///   Timer t;
alpar@2408
   245
  ///   doSomething();
alpar@2543
   246
  ///   std::cout << t << '\n';
alpar@2543
   247
  ///   t.restart();
alpar@2408
   248
  ///   doSomethingElse();
alpar@2543
   249
  ///   std::cout << t << '\n';
alpar@458
   250
  ///
alpar@2408
   251
  ///   ...
alpar@458
   252
  ///
alpar@2408
   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@2543
   264
  ///Also, if you start and stop the timer very frequently, it could lead to
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@2543
   351
    ///This function stops immediately the time counters, i.e. <tt>t.stop()</tt>
alpar@2543
   352
    ///is a faster
alpar@2543
   353
    ///equivalent of the following.
alpar@2543
   354
    ///\code
alpar@2543
   355
    ///  while(t.running()) t.stop()
alpar@2543
   356
    ///\endcode
alpar@2543
   357
    ///
alpar@1847
   358
    ///
alpar@1847
   359
    ///\sa stop()
alpar@1847
   360
    ///\sa restart()
alpar@1847
   361
    ///\sa reset()
alpar@1847
   362
alpar@1847
   363
    void halt() 
alpar@1847
   364
    {
alpar@1847
   365
      if(_running) {
alpar@1847
   366
	_running=0;
alpar@1847
   367
	TimeStamp t;
alpar@1847
   368
	t.stamp();
alpar@1847
   369
	start_time=t-start_time;
alpar@1847
   370
      }
alpar@1847
   371
    }
alpar@1847
   372
alpar@1847
   373
    ///Returns the running state of the timer
alpar@1847
   374
alpar@1847
   375
    ///This function returns the number of stop() exections that is
alpar@1847
   376
    ///necessary to really stop the timer.
alpar@1847
   377
    ///For example the timer
alpar@1847
   378
    ///is running if and only if the return value is \c true
alpar@1847
   379
    ///(i.e. greater than
alpar@1847
   380
    ///zero).
alpar@1847
   381
    int running()  { return _running; }
alpar@1847
   382
    
alpar@1847
   383
    
alpar@1847
   384
    ///Restart the time counters
alpar@1847
   385
alpar@1847
   386
    ///This function is a shorthand for
alpar@1847
   387
    ///a reset() and a start() calls.
alpar@1847
   388
    ///
alpar@1847
   389
    void restart() 
alpar@1847
   390
    {
alpar@1847
   391
      reset();
alpar@1847
   392
      start();
alpar@1847
   393
    }
alpar@1780
   394
    
alpar@1851
   395
    ///@}
alpar@1851
   396
alpar@1851
   397
    ///\name Query Functions for the ellapsed time
alpar@1851
   398
alpar@1851
   399
    ///@{
alpar@1851
   400
alpar@1005
   401
    ///Gives back the ellapsed user time of the process
alpar@1689
   402
    double userTime() const
alpar@1005
   403
    {
alpar@1689
   404
      return operator TimeStamp().userTime();
alpar@1005
   405
    }
alpar@1005
   406
    ///Gives back the ellapsed system time of the process
alpar@1689
   407
    double systemTime() const
alpar@1005
   408
    {
alpar@1689
   409
      return operator TimeStamp().systemTime();
alpar@1005
   410
    }
alpar@1005
   411
    ///Gives back the ellapsed user time of the process' children
alpar@1689
   412
    double cUserTime() const
alpar@1005
   413
    {
alpar@1689
   414
      return operator TimeStamp().cUserTime();
alpar@1005
   415
    }
alpar@1005
   416
    ///Gives back the ellapsed user time of the process' children
alpar@1689
   417
    double cSystemTime() const
alpar@1005
   418
    {
alpar@1689
   419
      return operator TimeStamp().cSystemTime();
alpar@1005
   420
    }
alpar@1780
   421
    ///Gives back the ellapsed real time
alpar@1689
   422
    double realTime() const
alpar@1005
   423
    {
alpar@1689
   424
      return operator TimeStamp().realTime();
alpar@1005
   425
    }
alpar@1851
   426
    ///Computes the ellapsed time
alpar@1005
   427
alpar@1851
   428
    ///This conversion computes the ellapsed time, therefore you can print
alpar@1851
   429
    ///the ellapsed time like this.
alpar@1851
   430
    ///\code
alpar@2543
   431
    ///  Timer t;
alpar@1851
   432
    ///  doSomething();
alpar@2543
   433
    ///  std::cout << t << '\n';
alpar@1851
   434
    ///\endcode
alpar@1851
   435
    operator TimeStamp () const
alpar@1851
   436
    {
alpar@1851
   437
      TimeStamp t;
alpar@1851
   438
      t.stamp();
alpar@1851
   439
      return _running?t-start_time:start_time;
alpar@1851
   440
    }
alpar@1851
   441
alpar@1851
   442
alpar@1851
   443
    ///@}
alpar@428
   444
  };
alpar@428
   445
alpar@1847
   446
  ///Same as \ref Timer but prints a report on destruction.
alpar@1847
   447
alpar@1847
   448
  ///Same as \ref Timer but prints a report on destruction.
alpar@1851
   449
  ///This example shows its usage.
alpar@1851
   450
  ///\code
alpar@1851
   451
  ///  void myAlg(ListGraph &g,int n)
alpar@1851
   452
  ///  {
alpar@2543
   453
  ///    TimeReport tr("Running time of myAlg: ");
alpar@1851
   454
  ///    ... //Here comes the algorithm
alpar@1851
   455
  ///  }
alpar@1851
   456
  ///\endcode
alpar@1851
   457
  ///
alpar@1851
   458
  ///\sa Timer
alpar@1851
   459
  ///\sa NoTimeReport
alpar@1851
   460
  ///\todo There is no test case for this
alpar@1847
   461
  class TimeReport : public Timer 
alpar@1847
   462
  {
alpar@1847
   463
    std::string _title;
alpar@1847
   464
    std::ostream &_os;
alpar@1847
   465
  public:
alpar@1847
   466
    ///\e
alpar@1851
   467
alpar@1851
   468
    ///\param title This text will be printed before the ellapsed time.
alpar@1851
   469
    ///\param os The stream to print the report to.
alpar@1851
   470
    ///\param run Sets whether the timer should start immediately.
alpar@1851
   471
alpar@1851
   472
    TimeReport(std::string title,std::ostream &os=std::cerr,bool run=true) 
alpar@1847
   473
      : Timer(run), _title(title), _os(os){}
alpar@1851
   474
    ///\e Prints the ellapsed time on destruction.
alpar@1847
   475
    ~TimeReport() 
alpar@1847
   476
    {
alpar@1851
   477
      _os << _title << *this << std::endl;
alpar@1847
   478
    }
alpar@1847
   479
  };
alpar@1847
   480
      
alpar@1851
   481
  ///'Do nothing' version of \ref TimeReport
alpar@428
   482
alpar@1851
   483
  ///\sa TimeReport
alpar@428
   484
  ///
alpar@1851
   485
  class NoTimeReport
alpar@428
   486
  {
alpar@1851
   487
  public:
alpar@1851
   488
    ///\e
alpar@1855
   489
    NoTimeReport(std::string,std::ostream &,bool) {}
alpar@1855
   490
    ///\e
alpar@1855
   491
    NoTimeReport(std::string,std::ostream &) {}
alpar@1855
   492
    ///\e
alpar@1855
   493
    NoTimeReport(std::string) {}
alpar@1851
   494
    ///\e Do nothing.
alpar@1851
   495
    ~NoTimeReport() {}
alpar@428
   496
alpar@1851
   497
    operator TimeStamp () const { return TimeStamp(); }
alpar@1851
   498
    void reset() {}
alpar@1851
   499
    void start() {}
alpar@1851
   500
    void stop() {}
alpar@1851
   501
    void halt() {} 
alpar@1851
   502
    int running() { return 0; }
alpar@1851
   503
    void restart() {}
alpar@1851
   504
    double userTime() const { return 0; }
alpar@1851
   505
    double systemTime() const { return 0; }
alpar@1851
   506
    double cUserTime() const { return 0; }
alpar@1851
   507
    double cSystemTime() const { return 0; }
alpar@1851
   508
    double realTime() const { return 0; }
alpar@1851
   509
  };
alpar@1851
   510
      
alpar@1689
   511
  ///Tool to measure the running time more exactly.
alpar@1689
   512
  
alpar@1689
   513
  ///This function calls \c f several times and returns the average
alpar@1689
   514
  ///running time. The number of the executions will be choosen in such a way
alpar@1780
   515
  ///that the full real running time will be roughly between \c min_time
alpar@1689
   516
  ///and <tt>2*min_time</tt>.
alpar@1689
   517
  ///\param f the function object to be measured.
alpar@1689
   518
  ///\param min_time the minimum total running time.
alpar@1894
   519
  ///\retval num if it is not \c NULL, then the actual
alpar@1894
   520
  ///        number of execution of \c f will be written into <tt>*num</tt>.
alpar@1894
   521
  ///\retval full_time if it is not \c NULL, then the actual
alpar@1894
   522
  ///        total running time will be written into <tt>*full_time</tt>.
alpar@1689
   523
  ///\return The average running time of \c f.
alpar@1689
   524
  
alpar@1689
   525
  template<class F>
alpar@2243
   526
  TimeStamp runningTimeTest(F f,double min_time=10,unsigned int *num = NULL,
deba@2027
   527
                            TimeStamp *full_time=NULL)
alpar@1689
   528
  {
alpar@2243
   529
    TimeStamp full;
alpar@2243
   530
    unsigned int total=0;
alpar@1689
   531
    Timer t;
alpar@2250
   532
    for(unsigned int tn=1;tn <= 1U<<31 && full.realTime()<=min_time; tn*=2) {
alpar@1811
   533
      for(;total<tn;total++) f();
alpar@1689
   534
      full=t;
alpar@1689
   535
    }
alpar@2243
   536
    if(num) *num=total;
alpar@2243
   537
    if(full_time) *full_time=full;
alpar@2243
   538
    return full/total;
alpar@1689
   539
  }
alpar@1689
   540
  
alpar@428
   541
  /// @}  
alpar@428
   542
alpar@1689
   543
alpar@921
   544
} //namespace lemon
alpar@428
   545
alpar@921
   546
#endif //LEMON_TIME_MEASURE_H