lemon/time_measure.h
author deba
Wed, 05 Oct 2005 13:19:30 +0000
changeset 1706 163746ec3094
parent 1435 8e85e6bbefdf
child 1780 9f052750753f
permissions -rw-r--r--
Removing NeedCopy
alpar@906
     1
/* -*- C++ -*-
ladanyi@1435
     2
 * lemon/time_measure.h - Part of LEMON, a generic C++ optimization library
alpar@906
     3
 *
alpar@1164
     4
 * Copyright (C) 2005 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
klao@491
    20
///\ingroup misc
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@428
    32
  /// \addtogroup misc
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@458
    47
  /// In most cases, perhaps \ref Timer class is what you want to use instead.
alpar@458
    48
  ///
alpar@458
    49
  ///\author Alpar Juttner
alpar@428
    50
alpar@428
    51
  class TimeStamp
alpar@428
    52
  {
alpar@1689
    53
    struct rtms 
alpar@1689
    54
    {
alpar@1689
    55
      double tms_utime;
alpar@1689
    56
      double tms_stime;
alpar@1689
    57
      double tms_cutime;
alpar@1689
    58
      double tms_cstime;
alpar@1689
    59
      rtms() {}
alpar@1689
    60
      rtms(tms ts) : tms_utime(ts.tms_utime), tms_stime(ts.tms_stime),
alpar@1689
    61
		     tms_cutime(ts.tms_cutime), tms_cstime(ts.tms_cstime) {}
alpar@1689
    62
    };
alpar@1689
    63
    rtms ts;
alpar@428
    64
    double real_time;
alpar@428
    65
  
alpar@1689
    66
    rtms &getTms() {return ts;}
alpar@1689
    67
    const rtms &getTms() const {return ts;}
alpar@1689
    68
alpar@428
    69
  public:
alpar@428
    70
alpar@428
    71
    ///Read the current time values of the process
alpar@428
    72
    void stamp()
alpar@428
    73
    {
alpar@428
    74
      timeval tv;
alpar@1689
    75
      tms _ts;
alpar@1689
    76
      times(&_ts);
alpar@428
    77
      gettimeofday(&tv, 0);real_time=tv.tv_sec+double(tv.tv_usec)/1e6;
alpar@1689
    78
      ts=_ts;
alpar@428
    79
    }
alpar@428
    80
  
alpar@428
    81
    /// Constructor initializing with zero
alpar@428
    82
    TimeStamp()
alpar@428
    83
    { ts.tms_utime=ts.tms_stime=ts.tms_cutime=ts.tms_cstime=0; real_time=0;}
alpar@428
    84
    ///Constructor initializing with the current time values of the process
alpar@428
    85
    TimeStamp(void *) { stamp();}
alpar@428
    86
  
alpar@1005
    87
    ///\e
alpar@428
    88
    TimeStamp &operator+=(const TimeStamp &b)
alpar@428
    89
    {
alpar@428
    90
      ts.tms_utime+=b.ts.tms_utime;
alpar@428
    91
      ts.tms_stime+=b.ts.tms_stime;
alpar@428
    92
      ts.tms_cutime+=b.ts.tms_cutime;
alpar@428
    93
      ts.tms_cstime+=b.ts.tms_cstime;
alpar@428
    94
      real_time+=b.real_time;
alpar@428
    95
      return *this;
alpar@428
    96
    }
alpar@1005
    97
    ///\e
alpar@428
    98
    TimeStamp operator+(const TimeStamp &b) const
alpar@428
    99
    {
alpar@428
   100
      TimeStamp t(*this);
alpar@428
   101
      return t+=b;
alpar@428
   102
    }
alpar@1005
   103
    ///\e
alpar@428
   104
    TimeStamp &operator-=(const TimeStamp &b)
alpar@428
   105
    {
alpar@428
   106
      ts.tms_utime-=b.ts.tms_utime;
alpar@428
   107
      ts.tms_stime-=b.ts.tms_stime;
alpar@428
   108
      ts.tms_cutime-=b.ts.tms_cutime;
alpar@428
   109
      ts.tms_cstime-=b.ts.tms_cstime;
alpar@428
   110
      real_time-=b.real_time;
alpar@428
   111
      return *this;
alpar@428
   112
    }
alpar@1005
   113
    ///\e
alpar@428
   114
    TimeStamp operator-(const TimeStamp &b) const
alpar@428
   115
    {
alpar@428
   116
      TimeStamp t(*this);
alpar@428
   117
      return t-=b;
alpar@428
   118
    }
alpar@1689
   119
    ///\e
alpar@1689
   120
alpar@1689
   121
    ///\bug operator * and / gives rounded values!
alpar@1689
   122
    TimeStamp &operator*=(double b)
alpar@1689
   123
    {
alpar@1689
   124
      ts.tms_utime*=b;
alpar@1689
   125
      ts.tms_stime*=b;
alpar@1689
   126
      ts.tms_cutime*=b;
alpar@1689
   127
      ts.tms_cstime*=b;
alpar@1689
   128
      real_time*=b;
alpar@1689
   129
      return *this;
alpar@1689
   130
    }
alpar@1689
   131
    ///\e
alpar@1689
   132
    TimeStamp operator*(double b) const
alpar@1689
   133
    {
alpar@1689
   134
      TimeStamp t(*this);
alpar@1689
   135
      return t*=b;
alpar@1689
   136
    }
alpar@1689
   137
    friend TimeStamp operator*(double b,const TimeStamp &t);
alpar@1689
   138
    ///\e
alpar@1689
   139
    TimeStamp &operator/=(double b)
alpar@1689
   140
    {
alpar@1689
   141
      ts.tms_utime/=b;
alpar@1689
   142
      ts.tms_stime/=b;
alpar@1689
   143
      ts.tms_cutime/=b;
alpar@1689
   144
      ts.tms_cstime/=b;
alpar@1689
   145
      real_time/=b;
alpar@1689
   146
      return *this;
alpar@1689
   147
    }
alpar@1689
   148
    ///\e
alpar@1689
   149
    TimeStamp operator/(double b) const
alpar@1689
   150
    {
alpar@1689
   151
      TimeStamp t(*this);
alpar@1689
   152
      return t/=b;
alpar@1689
   153
    }
alpar@428
   154
    ///The time ellapsed since the last call of stamp()
alpar@428
   155
    TimeStamp ellapsed() const
alpar@428
   156
    {
alpar@428
   157
      TimeStamp t(NULL);
alpar@428
   158
      return t-*this;
alpar@428
   159
    }
alpar@428
   160
  
alpar@428
   161
    friend std::ostream& operator<<(std::ostream& os,const TimeStamp &t);
alpar@428
   162
  
alpar@428
   163
    ///Gives back the user time of the process
alpar@1689
   164
    double userTime() const
alpar@428
   165
    {
alpar@428
   166
      return double(ts.tms_utime)/sysconf(_SC_CLK_TCK);
alpar@428
   167
    }
alpar@428
   168
    ///Gives back the system time of the process
alpar@1689
   169
    double systemTime() const
alpar@428
   170
    {
alpar@428
   171
      return double(ts.tms_stime)/sysconf(_SC_CLK_TCK);
alpar@428
   172
    }
alpar@428
   173
    ///Gives back the user time of the process' children
alpar@1689
   174
    double cUserTime() const
alpar@428
   175
    {
alpar@428
   176
      return double(ts.tms_cutime)/sysconf(_SC_CLK_TCK);
alpar@428
   177
    }
alpar@428
   178
    ///Gives back the user time of the process' children
alpar@1689
   179
    double cSystemTime() const
alpar@428
   180
    {
alpar@428
   181
      return double(ts.tms_cstime)/sysconf(_SC_CLK_TCK);
alpar@428
   182
    }
alpar@428
   183
    ///Gives back the real time of the process
alpar@1689
   184
    double realTime() const {return real_time;}
alpar@428
   185
  };
alpar@428
   186
alpar@1689
   187
  TimeStamp operator*(double b,const TimeStamp &t) 
alpar@1689
   188
  {
alpar@1689
   189
    return t*b;
alpar@1689
   190
  }
alpar@1689
   191
  
alpar@428
   192
  ///Class measuring the cpu time and real time usage of the process
alpar@458
   193
alpar@458
   194
  ///Class measuring the cpu time and real time usage of the process.
alpar@458
   195
  ///It is quite easy-to-use, here is a short example.
alpar@458
   196
  ///\code
alpar@921
   197
  ///#include<lemon/time_measure.h>
alpar@696
   198
  ///#include<iostream>
alpar@814
   199
  ///
alpar@458
   200
  ///int main()
alpar@458
   201
  ///{
alpar@458
   202
  ///
alpar@458
   203
  ///  ...
alpar@458
   204
  ///
alpar@696
   205
  ///  Timer T;
alpar@458
   206
  ///  doSomething();
alpar@696
   207
  ///  std::cout << T << '\n';
alpar@458
   208
  ///  T.reset();
alpar@458
   209
  ///  doSomethingElse();
alpar@696
   210
  ///  std::cout << T << '\n';
alpar@458
   211
  ///
alpar@458
   212
  ///  ...
alpar@458
   213
  ///
alpar@458
   214
  ///}
alpar@458
   215
  ///\endcode
alpar@458
   216
  ///
alpar@458
   217
  ///\todo This shouldn't be Unix (Linux) specific.
alpar@458
   218
  ///
alpar@458
   219
  ///\author Alpar Juttner
alpar@428
   220
  class Timer
alpar@428
   221
  {
alpar@428
   222
    TimeStamp start_time;
alpar@428
   223
alpar@428
   224
    void _reset() {start_time.stamp();}
alpar@428
   225
  
alpar@428
   226
  public: 
alpar@428
   227
    ///Constructor. It starts with zero time counters
alpar@428
   228
    Timer() {_reset();}
alpar@428
   229
alpar@428
   230
    ///Computes the ellapsed time
alpar@428
   231
alpar@428
   232
    ///This conversion computes the ellapsed time
alpar@428
   233
    ///since the construction of \c t or since
alpar@428
   234
    ///the last \c t.reset().
alpar@1005
   235
    operator TimeStamp () const
alpar@428
   236
    {
alpar@428
   237
      TimeStamp t;
alpar@428
   238
      t.stamp();
alpar@428
   239
      return t-start_time;
alpar@428
   240
    }
alpar@428
   241
alpar@428
   242
    ///Resets the time counters
alpar@1069
   243
alpar@1069
   244
    ///Resets the time counters
alpar@1069
   245
    ///
alpar@1069
   246
    void reset()
alpar@428
   247
    {
alpar@428
   248
      _reset();
alpar@428
   249
    }
alpar@1005
   250
alpar@1005
   251
alpar@1005
   252
    ///Gives back the ellapsed user time of the process
alpar@1689
   253
    double userTime() const
alpar@1005
   254
    {
alpar@1689
   255
      return operator TimeStamp().userTime();
alpar@1005
   256
    }
alpar@1005
   257
    ///Gives back the ellapsed system time of the process
alpar@1689
   258
    double systemTime() const
alpar@1005
   259
    {
alpar@1689
   260
      return operator TimeStamp().systemTime();
alpar@1005
   261
    }
alpar@1005
   262
    ///Gives back the ellapsed user time of the process' children
alpar@1689
   263
    double cUserTime() const
alpar@1005
   264
    {
alpar@1689
   265
      return operator TimeStamp().cUserTime();
alpar@1005
   266
    }
alpar@1005
   267
    ///Gives back the ellapsed user time of the process' children
alpar@1689
   268
    double cSystemTime() const
alpar@1005
   269
    {
alpar@1689
   270
      return operator TimeStamp().cSystemTime();
alpar@1005
   271
    }
alpar@1005
   272
    ///Gives back the ellapsed real time of the process
alpar@1689
   273
    double realTime() const
alpar@1005
   274
    {
alpar@1689
   275
      return operator TimeStamp().realTime();
alpar@1005
   276
    }
alpar@1005
   277
alpar@428
   278
  };
alpar@428
   279
alpar@428
   280
  ///Prints the time counters
alpar@428
   281
klao@492
   282
  ///Prints the time counters in the following form:
alpar@428
   283
  ///
alpar@440
   284
  /// <tt>u: XX.XXs s: XX.XXs cu: XX.XXs cs: XX.XXs real: XX.XXs</tt>
alpar@428
   285
  ///
alpar@428
   286
  /// where the values are the
alpar@440
   287
  /// \li \c u: user cpu time,
alpar@440
   288
  /// \li \c s: system cpu time,
alpar@440
   289
  /// \li \c cu: user cpu time of children,
alpar@440
   290
  /// \li \c cs: system cpu time of children,
alpar@440
   291
  /// \li \c real: real time.
alpar@814
   292
  /// \relates TimeStamp
alpar@428
   293
  inline std::ostream& operator<<(std::ostream& os,const TimeStamp &t)
alpar@428
   294
  {
alpar@428
   295
    long cls = sysconf(_SC_CLK_TCK);
alpar@428
   296
    os << "u: " << double(t.getTms().tms_utime)/cls <<
alpar@428
   297
      "s, s: " << double(t.getTms().tms_stime)/cls <<
alpar@428
   298
      "s, cu: " << double(t.getTms().tms_cutime)/cls <<
alpar@428
   299
      "s, cs: " << double(t.getTms().tms_cstime)/cls <<
alpar@1689
   300
      "s, real: " << t.realTime() << "s";
alpar@428
   301
    return os;
alpar@428
   302
  }
alpar@428
   303
alpar@1689
   304
  
alpar@1689
   305
  ///Tool to measure the running time more exactly.
alpar@1689
   306
  
alpar@1689
   307
  ///This function calls \c f several times and returns the average
alpar@1689
   308
  ///running time. The number of the executions will be choosen in such a way
alpar@1689
   309
  ///that the full running time will be roughly between \c min_time
alpar@1689
   310
  ///and <tt>2*min_time</tt>.
alpar@1689
   311
  ///\param f the function object to be measured.
alpar@1689
   312
  ///\param min_time the minimum total running time.
alpar@1689
   313
  ///\retval num if it is not \c NULL, then *num will contain the actual
alpar@1689
   314
  ///        number of execution of \c f.
alpar@1689
   315
  ///\retval full_time if it is not \c NULL, then *full_time
alpar@1689
   316
  ///        will contain the actual
alpar@1689
   317
  ///        total running time.
alpar@1689
   318
  ///\return The average running time of \c f.
alpar@1689
   319
  
alpar@1689
   320
  template<class F>
alpar@1689
   321
  TimeStamp runningTimeTest(F &f,double min_time=10,int *num = NULL,
alpar@1689
   322
			TimeStamp *full_time=NULL)
alpar@1689
   323
  {
alpar@1689
   324
    Timer t;
alpar@1689
   325
    TimeStamp full;
alpar@1689
   326
    int total=0;
alpar@1689
   327
    for(int tn=1;tn < 1<<24; tn*=2) {
alpar@1689
   328
      for(;total<tn;total++) f();
alpar@1689
   329
      full=t;
alpar@1689
   330
      if(full.realTime()>min_time) {
alpar@1689
   331
	if(num) *num=total;
alpar@1689
   332
	if(full_time) *full_time=full;
alpar@1689
   333
      return full/total;
alpar@1689
   334
      }
alpar@1689
   335
    }
alpar@1689
   336
    return TimeStamp();
alpar@1689
   337
  }
alpar@1689
   338
  
alpar@428
   339
  /// @}  
alpar@428
   340
alpar@1689
   341
alpar@921
   342
} //namespace lemon
alpar@428
   343
alpar@921
   344
#endif //LEMON_TIME_MEASURE_H