2 #ifndef HUGO_TIME_MEASURE_H
3 #define HUGO_TIME_MEASURE_H
13 // double currTime() {
16 // gettimeofday(&tv, 0);
17 // return double(tv.tv_sec)+double(tv.tv_usec)/1000000.0;
20 /// Class to store (cpu)time instances.
22 /// This class stores five time values.
25 /// - a system cpu time
26 /// - a user cpu time of children
27 /// - a system cpu time of children
29 /// TimeStamp's can be added to or substracted from each other and
30 /// they can be push to a stream.
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.
47 gettimeofday(&tv, 0);real_time=tv.tv_sec+double(tv.tv_usec)/1e6;
50 /// Constructor initializing with zero.
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();}
56 TimeStamp &operator+=(const TimeStamp &b)
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;
65 TimeStamp operator+(const TimeStamp &b) const
70 TimeStamp &operator-=(const TimeStamp &b)
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;
79 TimeStamp operator-(const TimeStamp &b) const
85 ///The time ellapsed since the last call of stamp()
86 TimeStamp ellapsed() const
92 friend std::ostream& operator<<(std::ostream& os,const TimeStamp &t);
94 double getUserTime() const
96 return double(ts.tms_utime)/sysconf(_SC_CLK_TCK);
98 double getSystemTime() const
100 return double(ts.tms_stime)/sysconf(_SC_CLK_TCK);
102 double getCUserTime() const
104 return double(ts.tms_cutime)/sysconf(_SC_CLK_TCK);
106 double getCSystemTime() const
108 return double(ts.tms_cstime)/sysconf(_SC_CLK_TCK);
112 ///Class measuring the cpu time and real time usage of the process.
115 TimeStamp start_time;
117 void _reset() {start_time.stamp();}
120 ///Constructor. It starts with zero time counters.
123 ///Computes the ellapsed time.
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 ()
135 ///Resets the time counters.
138 TimeStamp t(start_time);
144 ///Prints the time counters.
145 inline std::ostream& operator<<(std::ostream& os,const TimeStamp &t)
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";
158 #endif //HUGO_TIME_MEASURE_H