COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/work/marci/time_measure.h @ 381:d72470496fbe

Last change on this file since 381:d72470496fbe was 327:63c138adc477, checked in by Alpar Juttner, 20 years ago

Documentation.

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