Document the file itself.
2 #ifndef HUGO_TIME_MEASURE_H
3 #define HUGO_TIME_MEASURE_H
7 ///\brief Tools for measuring cpu usage
10 #include <sys/times.h>
20 /// A 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 pushed to a stream.
32 /// In most cases, perhaps \ref Timer class is what you want to use instead.
34 ///\author Alpar Juttner
43 tms &getTms() {return ts;}
44 const tms &getTms() const {return ts;}
45 ///Read the current time values of the process
50 gettimeofday(&tv, 0);real_time=tv.tv_sec+double(tv.tv_usec)/1e6;
53 /// Constructor initializing with zero
55 { ts.tms_utime=ts.tms_stime=ts.tms_cutime=ts.tms_cstime=0; real_time=0;}
56 ///Constructor initializing with the current time values of the process
57 TimeStamp(void *) { stamp();}
60 TimeStamp &operator+=(const TimeStamp &b)
62 ts.tms_utime+=b.ts.tms_utime;
63 ts.tms_stime+=b.ts.tms_stime;
64 ts.tms_cutime+=b.ts.tms_cutime;
65 ts.tms_cstime+=b.ts.tms_cstime;
66 real_time+=b.real_time;
70 TimeStamp operator+(const TimeStamp &b) const
76 TimeStamp &operator-=(const TimeStamp &b)
78 ts.tms_utime-=b.ts.tms_utime;
79 ts.tms_stime-=b.ts.tms_stime;
80 ts.tms_cutime-=b.ts.tms_cutime;
81 ts.tms_cstime-=b.ts.tms_cstime;
82 real_time-=b.real_time;
86 TimeStamp operator-(const TimeStamp &b) const
92 ///The time ellapsed since the last call of stamp()
93 TimeStamp ellapsed() const
99 friend std::ostream& operator<<(std::ostream& os,const TimeStamp &t);
101 ///Gives back the user time of the process
102 double getUserTime() const
104 return double(ts.tms_utime)/sysconf(_SC_CLK_TCK);
106 ///Gives back the system time of the process
107 double getSystemTime() const
109 return double(ts.tms_stime)/sysconf(_SC_CLK_TCK);
111 ///Gives back the user time of the process' children
112 double getCUserTime() const
114 return double(ts.tms_cutime)/sysconf(_SC_CLK_TCK);
116 ///Gives back the user time of the process' children
117 double getCSystemTime() const
119 return double(ts.tms_cstime)/sysconf(_SC_CLK_TCK);
121 ///Gives back the real time of the process
122 double getRealTime() const {return real_time;}
125 ///Class measuring the cpu time and real time usage of the process
127 ///Class measuring the cpu time and real time usage of the process.
128 ///It is quite easy-to-use, here is a short example.
130 ///#include<hugo/time_measure.h>
131 ///#include<iostream>
140 /// std::cout << T << '\n';
142 /// doSomethingElse();
143 /// std::cout << T << '\n';
150 ///\todo This shouldn't be Unix (Linux) specific.
152 ///\author Alpar Juttner
155 TimeStamp start_time;
157 void _reset() {start_time.stamp();}
160 ///Constructor. It starts with zero time counters
163 ///Computes the ellapsed time
165 ///This conversion computes the ellapsed time
166 ///since the construction of \c t or since
167 ///the last \c t.reset().
168 operator TimeStamp ()
175 ///Resets the time counters
178 TimeStamp t(start_time);
184 ///Prints the time counters
186 ///Prints the time counters in the following form:
188 /// <tt>u: XX.XXs s: XX.XXs cu: XX.XXs cs: XX.XXs real: XX.XXs</tt>
190 /// where the values are the
191 /// \li \c u: user cpu time,
192 /// \li \c s: system cpu time,
193 /// \li \c cu: user cpu time of children,
194 /// \li \c cs: system cpu time of children,
195 /// \li \c real: real time.
196 /// \relates TimeStamp
197 inline std::ostream& operator<<(std::ostream& os,const TimeStamp &t)
199 long cls = sysconf(_SC_CLK_TCK);
200 os << "u: " << double(t.getTms().tms_utime)/cls <<
201 "s, s: " << double(t.getTms().tms_stime)/cls <<
202 "s, cu: " << double(t.getTms().tms_cutime)/cls <<
203 "s, cs: " << double(t.getTms().tms_cstime)/cls <<
204 "s, real: " << t.getRealTime() << "s";
212 #endif //HUGO_TIME_MEASURE_H