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