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