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