Mapselector widget reached its first release, but there are still work to do on it, I know...
2 * lemon/time_measure.h - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Research Group on Combinatorial Optimization, EGRES).
7 * Permission to use, modify and distribute this software is granted
8 * provided that this copyright notice appears in all copies. For
9 * precise terms see the accompanying LICENSE file.
11 * This software is provided "AS IS" with no warranty of any kind,
12 * express or implied, and with no claim as to its suitability for any
17 #ifndef LEMON_TIME_MEASURE_H
18 #define LEMON_TIME_MEASURE_H
22 ///\brief Tools for measuring cpu usage
25 #include <sys/times.h>
35 /// A class to store (cpu)time instances.
37 /// This class stores five time values.
40 /// - a system cpu time
41 /// - a user cpu time of children
42 /// - a system cpu time of children
44 /// TimeStamp's can be added to or substracted from each other and
45 /// they can be pushed to a stream.
47 /// In most cases, perhaps \ref Timer class is what you want to use instead.
49 ///\author Alpar Juttner
60 rtms(tms ts) : tms_utime(ts.tms_utime), tms_stime(ts.tms_stime),
61 tms_cutime(ts.tms_cutime), tms_cstime(ts.tms_cstime) {}
66 rtms &getTms() {return ts;}
67 const rtms &getTms() const {return ts;}
71 ///Read the current time values of the process
77 gettimeofday(&tv, 0);real_time=tv.tv_sec+double(tv.tv_usec)/1e6;
81 /// Constructor initializing with zero
83 { ts.tms_utime=ts.tms_stime=ts.tms_cutime=ts.tms_cstime=0; real_time=0;}
84 ///Constructor initializing with the current time values of the process
85 TimeStamp(void *) { stamp();}
88 TimeStamp &operator+=(const TimeStamp &b)
90 ts.tms_utime+=b.ts.tms_utime;
91 ts.tms_stime+=b.ts.tms_stime;
92 ts.tms_cutime+=b.ts.tms_cutime;
93 ts.tms_cstime+=b.ts.tms_cstime;
94 real_time+=b.real_time;
98 TimeStamp operator+(const TimeStamp &b) const
104 TimeStamp &operator-=(const TimeStamp &b)
106 ts.tms_utime-=b.ts.tms_utime;
107 ts.tms_stime-=b.ts.tms_stime;
108 ts.tms_cutime-=b.ts.tms_cutime;
109 ts.tms_cstime-=b.ts.tms_cstime;
110 real_time-=b.real_time;
114 TimeStamp operator-(const TimeStamp &b) const
121 ///\bug operator * and / gives rounded values!
122 TimeStamp &operator*=(double b)
132 TimeStamp operator*(double b) const
137 friend TimeStamp operator*(double b,const TimeStamp &t);
139 TimeStamp &operator/=(double b)
149 TimeStamp operator/(double b) const
154 ///The time ellapsed since the last call of stamp()
155 TimeStamp ellapsed() const
161 friend std::ostream& operator<<(std::ostream& os,const TimeStamp &t);
163 ///Gives back the user time of the process
164 double userTime() const
166 return double(ts.tms_utime)/sysconf(_SC_CLK_TCK);
168 ///Gives back the system time of the process
169 double systemTime() const
171 return double(ts.tms_stime)/sysconf(_SC_CLK_TCK);
173 ///Gives back the user time of the process' children
174 double cUserTime() const
176 return double(ts.tms_cutime)/sysconf(_SC_CLK_TCK);
178 ///Gives back the user time of the process' children
179 double cSystemTime() const
181 return double(ts.tms_cstime)/sysconf(_SC_CLK_TCK);
183 ///Gives back the real time of the process
184 double realTime() const {return real_time;}
187 TimeStamp operator*(double b,const TimeStamp &t)
192 ///Class measuring the cpu time and real time usage of the process
194 ///Class measuring the cpu time and real time usage of the process.
195 ///It is quite easy-to-use, here is a short example.
197 ///#include<lemon/time_measure.h>
198 ///#include<iostream>
207 /// std::cout << T << '\n';
209 /// doSomethingElse();
210 /// std::cout << T << '\n';
217 ///\todo This shouldn't be Unix (Linux) specific.
219 ///\author Alpar Juttner
222 TimeStamp start_time;
224 void _reset() {start_time.stamp();}
227 ///Constructor. It starts with zero time counters
230 ///Computes the ellapsed time
232 ///This conversion computes the ellapsed time
233 ///since the construction of \c t or since
234 ///the last \c t.reset().
235 operator TimeStamp () const
242 ///Resets the time counters
244 ///Resets the time counters
252 ///Gives back the ellapsed user time of the process
253 double userTime() const
255 return operator TimeStamp().userTime();
257 ///Gives back the ellapsed system time of the process
258 double systemTime() const
260 return operator TimeStamp().systemTime();
262 ///Gives back the ellapsed user time of the process' children
263 double cUserTime() const
265 return operator TimeStamp().cUserTime();
267 ///Gives back the ellapsed user time of the process' children
268 double cSystemTime() const
270 return operator TimeStamp().cSystemTime();
272 ///Gives back the ellapsed real time of the process
273 double realTime() const
275 return operator TimeStamp().realTime();
280 ///Prints the time counters
282 ///Prints the time counters in the following form:
284 /// <tt>u: XX.XXs s: XX.XXs cu: XX.XXs cs: XX.XXs real: XX.XXs</tt>
286 /// where the values are the
287 /// \li \c u: user cpu time,
288 /// \li \c s: system cpu time,
289 /// \li \c cu: user cpu time of children,
290 /// \li \c cs: system cpu time of children,
291 /// \li \c real: real time.
292 /// \relates TimeStamp
293 inline std::ostream& operator<<(std::ostream& os,const TimeStamp &t)
295 long cls = sysconf(_SC_CLK_TCK);
296 os << "u: " << double(t.getTms().tms_utime)/cls <<
297 "s, s: " << double(t.getTms().tms_stime)/cls <<
298 "s, cu: " << double(t.getTms().tms_cutime)/cls <<
299 "s, cs: " << double(t.getTms().tms_cstime)/cls <<
300 "s, real: " << t.realTime() << "s";
305 ///Tool to measure the running time more exactly.
307 ///This function calls \c f several times and returns the average
308 ///running time. The number of the executions will be choosen in such a way
309 ///that the full running time will be roughly between \c min_time
310 ///and <tt>2*min_time</tt>.
311 ///\param f the function object to be measured.
312 ///\param min_time the minimum total running time.
313 ///\retval num if it is not \c NULL, then *num will contain the actual
314 /// number of execution of \c f.
315 ///\retval full_time if it is not \c NULL, then *full_time
316 /// will contain the actual
317 /// total running time.
318 ///\return The average running time of \c f.
321 TimeStamp runningTimeTest(F &f,double min_time=10,int *num = NULL,
322 TimeStamp *full_time=NULL)
327 for(int tn=1;tn < 1<<24; tn*=2) {
328 for(;total<tn;total++) f();
330 if(full.realTime()>min_time) {
332 if(full_time) *full_time=full;
344 #endif //LEMON_TIME_MEASURE_H