COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/work/marci/time_measure.h @ 404:d888ca4e6c00

Last change on this file since 404:d888ca4e6c00 was 327:63c138adc477, checked in by Alpar Juttner, 21 years ago

Documentation.

File size: 3.8 KB
Line 
1// -*- c++ -*-
2#ifndef HUGO_TIME_MEASURE_H
3#define HUGO_TIME_MEASURE_H
4
5#include <sys/time.h>
6#include <sys/times.h>
7#include <fstream>
8#include <iostream>
9#include <unistd.h>
10
11namespace hugo {
12
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
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
32  class TimeStamp
33  {
34    tms ts;
35    double real_time;
36 
37  public:
38
39    tms &getTms() {return ts;}
40    const tms &getTms() const {return ts;}
41    double getRealTime() const {return real_time;}
42    ///Read the current time values of the process.
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 
50    /// Constructor initializing with zero.
51    TimeStamp()
52    { ts.tms_utime=ts.tms_stime=ts.tms_cutime=ts.tms_cstime=0; real_time=0;}
53    ///Constructor initializing with the current time values of the process.
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
85    ///The time ellapsed since the last call of stamp()
86    TimeStamp ellapsed() const
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
112  ///Class measuring the cpu time and real time usage of the process.
113  class Timer
114  {
115    TimeStamp start_time;
116
117    void _reset() {start_time.stamp();}
118 
119  public:
120    ///Constructor. It starts with zero time counters.
121    Timer() {_reset();}
122
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().
128    operator TimeStamp ()
129    {
130      TimeStamp t;
131      t.stamp();
132      return t-start_time;
133    }
134
135    ///Resets the time counters.
136    TimeStamp reset()
137    {
138      TimeStamp t(start_time);
139      _reset();
140      return start_time-t;
141    }
142  };
143
144  ///Prints the time counters.
145  inline std::ostream& operator<<(std::ostream& os,const TimeStamp &t)
146  {
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;
154  }
155
156} //namespace hugo
157
158#endif //HUGO_TIME_MEASURE_H
Note: See TracBrowser for help on using the repository browser.