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