src/lemon/time_measure.h
author marci
Mon, 15 Nov 2004 16:39:55 +0000
changeset 997 665ffade9aca
parent 906 17f31d280385
child 1005 63ccf7136641
permissions -rw-r--r--
RevGraphWrapper modified according to the factory
alpar@906
     1
/* -*- C++ -*-
alpar@921
     2
 * src/lemon/time_measure.h - Part of LEMON, a generic C++ optimization library
alpar@906
     3
 *
alpar@906
     4
 * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@906
     5
 * (Egervary Combinatorial Optimization Research Group, 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@428
    53
    tms ts;
alpar@428
    54
    double real_time;
alpar@428
    55
  
alpar@428
    56
  public:
alpar@428
    57
alpar@428
    58
    tms &getTms() {return ts;}
alpar@428
    59
    const tms &getTms() const {return ts;}
alpar@428
    60
    ///Read the current time values of the process
alpar@428
    61
    void stamp()
alpar@428
    62
    {
alpar@428
    63
      timeval tv;
alpar@428
    64
      times(&ts);
alpar@428
    65
      gettimeofday(&tv, 0);real_time=tv.tv_sec+double(tv.tv_usec)/1e6;
alpar@428
    66
    }
alpar@428
    67
  
alpar@428
    68
    /// Constructor initializing with zero
alpar@428
    69
    TimeStamp()
alpar@428
    70
    { ts.tms_utime=ts.tms_stime=ts.tms_cutime=ts.tms_cstime=0; real_time=0;}
alpar@428
    71
    ///Constructor initializing with the current time values of the process
alpar@428
    72
    TimeStamp(void *) { stamp();}
alpar@428
    73
  
alpar@428
    74
    ///
alpar@428
    75
    TimeStamp &operator+=(const TimeStamp &b)
alpar@428
    76
    {
alpar@428
    77
      ts.tms_utime+=b.ts.tms_utime;
alpar@428
    78
      ts.tms_stime+=b.ts.tms_stime;
alpar@428
    79
      ts.tms_cutime+=b.ts.tms_cutime;
alpar@428
    80
      ts.tms_cstime+=b.ts.tms_cstime;
alpar@428
    81
      real_time+=b.real_time;
alpar@428
    82
      return *this;
alpar@428
    83
    }
alpar@428
    84
    ///
alpar@428
    85
    TimeStamp operator+(const TimeStamp &b) const
alpar@428
    86
    {
alpar@428
    87
      TimeStamp t(*this);
alpar@428
    88
      return t+=b;
alpar@428
    89
    }
alpar@428
    90
    ///
alpar@428
    91
    TimeStamp &operator-=(const TimeStamp &b)
alpar@428
    92
    {
alpar@428
    93
      ts.tms_utime-=b.ts.tms_utime;
alpar@428
    94
      ts.tms_stime-=b.ts.tms_stime;
alpar@428
    95
      ts.tms_cutime-=b.ts.tms_cutime;
alpar@428
    96
      ts.tms_cstime-=b.ts.tms_cstime;
alpar@428
    97
      real_time-=b.real_time;
alpar@428
    98
      return *this;
alpar@428
    99
    }
alpar@428
   100
    ///
alpar@428
   101
    TimeStamp operator-(const TimeStamp &b) const
alpar@428
   102
    {
alpar@428
   103
      TimeStamp t(*this);
alpar@428
   104
      return t-=b;
alpar@428
   105
    }
alpar@428
   106
alpar@428
   107
    ///The time ellapsed since the last call of stamp()
alpar@428
   108
    TimeStamp ellapsed() const
alpar@428
   109
    {
alpar@428
   110
      TimeStamp t(NULL);
alpar@428
   111
      return t-*this;
alpar@428
   112
    }
alpar@428
   113
  
alpar@428
   114
    friend std::ostream& operator<<(std::ostream& os,const TimeStamp &t);
alpar@428
   115
  
alpar@428
   116
    ///Gives back the user time of the process
alpar@428
   117
    double getUserTime() const
alpar@428
   118
    {
alpar@428
   119
      return double(ts.tms_utime)/sysconf(_SC_CLK_TCK);
alpar@428
   120
    }
alpar@428
   121
    ///Gives back the system time of the process
alpar@428
   122
    double getSystemTime() const
alpar@428
   123
    {
alpar@428
   124
      return double(ts.tms_stime)/sysconf(_SC_CLK_TCK);
alpar@428
   125
    }
alpar@428
   126
    ///Gives back the user time of the process' children
alpar@428
   127
    double getCUserTime() const
alpar@428
   128
    {
alpar@428
   129
      return double(ts.tms_cutime)/sysconf(_SC_CLK_TCK);
alpar@428
   130
    }
alpar@428
   131
    ///Gives back the user time of the process' children
alpar@428
   132
    double getCSystemTime() const
alpar@428
   133
    {
alpar@428
   134
      return double(ts.tms_cstime)/sysconf(_SC_CLK_TCK);
alpar@428
   135
    }
alpar@428
   136
    ///Gives back the real time of the process
alpar@428
   137
    double getRealTime() const {return real_time;}
alpar@428
   138
  };
alpar@428
   139
alpar@428
   140
  ///Class measuring the cpu time and real time usage of the process
alpar@458
   141
alpar@458
   142
  ///Class measuring the cpu time and real time usage of the process.
alpar@458
   143
  ///It is quite easy-to-use, here is a short example.
alpar@458
   144
  ///\code
alpar@921
   145
  ///#include<lemon/time_measure.h>
alpar@696
   146
  ///#include<iostream>
alpar@814
   147
  ///
alpar@458
   148
  ///int main()
alpar@458
   149
  ///{
alpar@458
   150
  ///
alpar@458
   151
  ///  ...
alpar@458
   152
  ///
alpar@696
   153
  ///  Timer T;
alpar@458
   154
  ///  doSomething();
alpar@696
   155
  ///  std::cout << T << '\n';
alpar@458
   156
  ///  T.reset();
alpar@458
   157
  ///  doSomethingElse();
alpar@696
   158
  ///  std::cout << T << '\n';
alpar@458
   159
  ///
alpar@458
   160
  ///  ...
alpar@458
   161
  ///
alpar@458
   162
  ///}
alpar@458
   163
  ///\endcode
alpar@458
   164
  ///
alpar@458
   165
  ///\todo This shouldn't be Unix (Linux) specific.
alpar@458
   166
  ///
alpar@458
   167
  ///\author Alpar Juttner
alpar@428
   168
  class Timer
alpar@428
   169
  {
alpar@428
   170
    TimeStamp start_time;
alpar@428
   171
alpar@428
   172
    void _reset() {start_time.stamp();}
alpar@428
   173
  
alpar@428
   174
  public: 
alpar@428
   175
    ///Constructor. It starts with zero time counters
alpar@428
   176
    Timer() {_reset();}
alpar@428
   177
alpar@428
   178
    ///Computes the ellapsed time
alpar@428
   179
alpar@428
   180
    ///This conversion computes the ellapsed time
alpar@428
   181
    ///since the construction of \c t or since
alpar@428
   182
    ///the last \c t.reset().
alpar@428
   183
    operator TimeStamp ()
alpar@428
   184
    {
alpar@428
   185
      TimeStamp t;
alpar@428
   186
      t.stamp();
alpar@428
   187
      return t-start_time;
alpar@428
   188
    }
alpar@428
   189
alpar@428
   190
    ///Resets the time counters
alpar@428
   191
    TimeStamp reset()
alpar@428
   192
    {
alpar@428
   193
      TimeStamp t(start_time);
alpar@428
   194
      _reset();
alpar@428
   195
      return start_time-t;
alpar@428
   196
    }
alpar@428
   197
  };
alpar@428
   198
alpar@428
   199
  ///Prints the time counters
alpar@428
   200
klao@492
   201
  ///Prints the time counters in the following form:
alpar@428
   202
  ///
alpar@440
   203
  /// <tt>u: XX.XXs s: XX.XXs cu: XX.XXs cs: XX.XXs real: XX.XXs</tt>
alpar@428
   204
  ///
alpar@428
   205
  /// where the values are the
alpar@440
   206
  /// \li \c u: user cpu time,
alpar@440
   207
  /// \li \c s: system cpu time,
alpar@440
   208
  /// \li \c cu: user cpu time of children,
alpar@440
   209
  /// \li \c cs: system cpu time of children,
alpar@440
   210
  /// \li \c real: real time.
alpar@814
   211
  /// \relates TimeStamp
alpar@428
   212
  inline std::ostream& operator<<(std::ostream& os,const TimeStamp &t)
alpar@428
   213
  {
alpar@428
   214
    long cls = sysconf(_SC_CLK_TCK);
alpar@428
   215
    os << "u: " << double(t.getTms().tms_utime)/cls <<
alpar@428
   216
      "s, s: " << double(t.getTms().tms_stime)/cls <<
alpar@428
   217
      "s, cu: " << double(t.getTms().tms_cutime)/cls <<
alpar@428
   218
      "s, cs: " << double(t.getTms().tms_cstime)/cls <<
alpar@428
   219
      "s, real: " << t.getRealTime() << "s";
alpar@428
   220
    return os;
alpar@428
   221
  }
alpar@428
   222
alpar@428
   223
  /// @}  
alpar@428
   224
alpar@921
   225
} //namespace lemon
alpar@428
   226
alpar@921
   227
#endif //LEMON_TIME_MEASURE_H