src/work/marci/time_measure.h
author jacint
Thu, 22 Apr 2004 15:56:05 +0000
changeset 374 0fc9cd9b854a
parent 324 c8b0ad782bda
child 425 4fbe868c1fb4
permissions -rw-r--r--
(none)
marci@73
     1
// -*- c++ -*-
marci@259
     2
#ifndef HUGO_TIME_MEASURE_H
marci@259
     3
#define HUGO_TIME_MEASURE_H
marci@73
     4
marci@73
     5
#include <sys/time.h>
alpar@117
     6
#include <sys/times.h>
alpar@117
     7
#include <fstream>
alpar@117
     8
#include <iostream>
jacint@121
     9
#include <unistd.h>
marci@73
    10
marci@323
    11
namespace hugo {
marci@73
    12
marci@323
    13
//   double currTime() {
marci@323
    14
//     timeval tv;
marci@323
    15
//     //timezone tz;
marci@323
    16
//     gettimeofday(&tv, 0);
marci@323
    17
//     return double(tv.tv_sec)+double(tv.tv_usec)/1000000.0;
marci@323
    18
//   }
marci@323
    19
alpar@327
    20
  /// Class to store (cpu)time instances.
alpar@327
    21
alpar@327
    22
  /// This class stores five time values.
alpar@327
    23
  /// - a real time
alpar@327
    24
  /// - a user cpu time
alpar@327
    25
  /// - a system cpu time
alpar@327
    26
  /// - a user cpu time of children
alpar@327
    27
  /// - a system cpu time of children
alpar@327
    28
  ///
alpar@327
    29
  /// TimeStamp's can be added to or substracted from each other and
alpar@327
    30
  /// they can be push to a stream.
alpar@327
    31
marci@323
    32
  class TimeStamp
marci@323
    33
  {
marci@323
    34
    tms ts;
marci@323
    35
    double real_time;
alpar@118
    36
  
marci@323
    37
  public:
alpar@117
    38
marci@323
    39
    tms &getTms() {return ts;}
marci@323
    40
    const tms &getTms() const {return ts;}
marci@323
    41
    double getRealTime() const {return real_time;}
alpar@327
    42
    ///Read the current time values of the process.
marci@323
    43
    void stamp()
marci@323
    44
    {
marci@323
    45
      timeval tv;
marci@323
    46
      times(&ts);
marci@323
    47
      gettimeofday(&tv, 0);real_time=tv.tv_sec+double(tv.tv_usec)/1e6;
marci@323
    48
    }
marci@323
    49
  
alpar@327
    50
    /// Constructor initializing with zero.
marci@323
    51
    TimeStamp()
marci@323
    52
    { ts.tms_utime=ts.tms_stime=ts.tms_cutime=ts.tms_cstime=0; real_time=0;}
alpar@327
    53
    ///Constructor initializing with the current time values of the process.
marci@323
    54
    TimeStamp(void *) { stamp();}
marci@323
    55
  
marci@323
    56
    TimeStamp &operator+=(const TimeStamp &b)
marci@323
    57
    {
marci@323
    58
      ts.tms_utime+=b.ts.tms_utime;
marci@323
    59
      ts.tms_stime+=b.ts.tms_stime;
marci@323
    60
      ts.tms_cutime+=b.ts.tms_cutime;
marci@323
    61
      ts.tms_cstime+=b.ts.tms_cstime;
marci@323
    62
      real_time+=b.real_time;
marci@323
    63
      return *this;
marci@323
    64
    }
marci@323
    65
    TimeStamp operator+(const TimeStamp &b) const
marci@323
    66
    {
marci@323
    67
      TimeStamp t(*this);
marci@323
    68
      return t+=b;
marci@323
    69
    }
marci@323
    70
    TimeStamp &operator-=(const TimeStamp &b)
marci@323
    71
    {
marci@323
    72
      ts.tms_utime-=b.ts.tms_utime;
marci@323
    73
      ts.tms_stime-=b.ts.tms_stime;
marci@323
    74
      ts.tms_cutime-=b.ts.tms_cutime;
marci@323
    75
      ts.tms_cstime-=b.ts.tms_cstime;
marci@323
    76
      real_time-=b.real_time;
marci@323
    77
      return *this;
marci@323
    78
    }
marci@323
    79
    TimeStamp operator-(const TimeStamp &b) const
marci@323
    80
    {
marci@323
    81
      TimeStamp t(*this);
marci@323
    82
      return t-=b;
marci@323
    83
    }
marci@323
    84
alpar@327
    85
    ///The time ellapsed since the last call of stamp()
alpar@324
    86
    TimeStamp ellapsed() const
marci@323
    87
    {
marci@323
    88
      TimeStamp t(NULL);
marci@323
    89
      return t-*this;
marci@323
    90
    }
marci@323
    91
  
marci@323
    92
    friend std::ostream& operator<<(std::ostream& os,const TimeStamp &t);
marci@323
    93
  
marci@323
    94
    double getUserTime() const
marci@323
    95
    {
marci@323
    96
      return double(ts.tms_utime)/sysconf(_SC_CLK_TCK);
marci@323
    97
    }
marci@323
    98
    double getSystemTime() const
marci@323
    99
    {
marci@323
   100
      return double(ts.tms_stime)/sysconf(_SC_CLK_TCK);
marci@323
   101
    }
marci@323
   102
    double getCUserTime() const
marci@323
   103
    {
marci@323
   104
      return double(ts.tms_cutime)/sysconf(_SC_CLK_TCK);
marci@323
   105
    }
marci@323
   106
    double getCSystemTime() const
marci@323
   107
    {
marci@323
   108
      return double(ts.tms_cstime)/sysconf(_SC_CLK_TCK);
marci@323
   109
    }
marci@323
   110
  };
marci@323
   111
alpar@327
   112
  ///Class measuring the cpu time and real time usage of the process.
marci@323
   113
  class Timer
alpar@118
   114
  {
marci@323
   115
    TimeStamp start_time;
marci@323
   116
marci@323
   117
    void _reset() {start_time.stamp();}
alpar@118
   118
  
marci@323
   119
  public: 
alpar@327
   120
    ///Constructor. It starts with zero time counters.
marci@323
   121
    Timer() {_reset();}
marci@323
   122
alpar@327
   123
    ///Computes the ellapsed time.
alpar@327
   124
alpar@327
   125
    ///This conversion computes the ellapsed time
alpar@327
   126
    ///since the construction of \c t or since
alpar@327
   127
    ///the last \c t.reset().
marci@323
   128
    operator TimeStamp ()
marci@323
   129
    {
marci@323
   130
      TimeStamp t;
marci@323
   131
      t.stamp();
marci@323
   132
      return t-start_time;
marci@323
   133
    }
marci@323
   134
alpar@327
   135
    ///Resets the time counters.
marci@323
   136
    TimeStamp reset()
marci@323
   137
    {
marci@323
   138
      TimeStamp t(start_time);
marci@323
   139
      _reset();
marci@323
   140
      return start_time-t;
marci@323
   141
    }
marci@323
   142
  };
marci@323
   143
alpar@327
   144
  ///Prints the time counters.
marci@323
   145
  inline std::ostream& operator<<(std::ostream& os,const TimeStamp &t)
alpar@117
   146
  {
marci@323
   147
    long cls = sysconf(_SC_CLK_TCK);
marci@323
   148
    os << "u: " << double(t.getTms().tms_utime)/cls <<
marci@323
   149
      "s, s: " << double(t.getTms().tms_stime)/cls <<
marci@323
   150
      "s, cu: " << double(t.getTms().tms_cutime)/cls <<
marci@323
   151
      "s, cs: " << double(t.getTms().tms_cstime)/cls <<
marci@323
   152
      "s, real: " << t.getRealTime() << "s";
marci@323
   153
    return os;
alpar@117
   154
  }
alpar@117
   155
marci@323
   156
} //namespace hugo
alpar@117
   157
marci@259
   158
#endif //HUGO_TIME_MEASURE_H