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;}
70 { ts.tms_utime=ts.tms_stime=ts.tms_cutime=ts.tms_cstime=0; real_time=0;}
74 ///Read the current time values of the process
80 gettimeofday(&tv, 0);real_time=tv.tv_sec+double(tv.tv_usec)/1e6;
84 /// Constructor initializing with zero
87 ///Constructor initializing with the current time values of the process
88 TimeStamp(void *) { stamp();}
90 ///Set every time value to zero
91 TimeStamp &reset() {_reset();return *this;}
94 TimeStamp &operator+=(const TimeStamp &b)
96 ts.tms_utime+=b.ts.tms_utime;
97 ts.tms_stime+=b.ts.tms_stime;
98 ts.tms_cutime+=b.ts.tms_cutime;
99 ts.tms_cstime+=b.ts.tms_cstime;
100 real_time+=b.real_time;
104 TimeStamp operator+(const TimeStamp &b) const
110 TimeStamp &operator-=(const TimeStamp &b)
112 ts.tms_utime-=b.ts.tms_utime;
113 ts.tms_stime-=b.ts.tms_stime;
114 ts.tms_cutime-=b.ts.tms_cutime;
115 ts.tms_cstime-=b.ts.tms_cstime;
116 real_time-=b.real_time;
120 TimeStamp operator-(const TimeStamp &b) const
126 TimeStamp &operator*=(double b)
136 TimeStamp operator*(double b) const
141 friend TimeStamp operator*(double b,const TimeStamp &t);
143 TimeStamp &operator/=(double b)
153 TimeStamp operator/(double b) const
158 ///The time ellapsed since the last call of stamp()
159 TimeStamp ellapsed() const
165 friend std::ostream& operator<<(std::ostream& os,const TimeStamp &t);
167 ///Gives back the user time of the process
168 double userTime() const
170 return double(ts.tms_utime)/sysconf(_SC_CLK_TCK);
172 ///Gives back the system time of the process
173 double systemTime() const
175 return double(ts.tms_stime)/sysconf(_SC_CLK_TCK);
177 ///Gives back the user time of the process' children
178 double cUserTime() const
180 return double(ts.tms_cutime)/sysconf(_SC_CLK_TCK);
182 ///Gives back the user time of the process' children
183 double cSystemTime() const
185 return double(ts.tms_cstime)/sysconf(_SC_CLK_TCK);
187 ///Gives back the real time
188 double realTime() const {return real_time;}
191 TimeStamp operator*(double b,const TimeStamp &t)
196 ///Class for measuring the cpu time and real time usage of the process
198 ///Class for measuring the cpu time and real time usage of the process.
199 ///It is quite easy-to-use, here is a short example.
201 ///#include<lemon/time_measure.h>
202 ///#include<iostream>
211 /// std::cout << T << '\n';
213 /// doSomethingElse();
214 /// std::cout << T << '\n';
221 ///The \ref Timer can also be \ref stop() "stopped" and
222 ///\ref start() "started" again, so it is easy to compute collected
225 ///\warning Depending on the operation system and its actual configuration
226 ///the time counters have a certain (relatively big) granularity.
227 ///Therefore this tool is not appropriate to measure very short times.
228 ///Also, if you start and stop the timer very frequently, it could lead
229 ///distorted results.
231 ///The \ref Timer also counts the number of \ref start()
232 ///executions, and is stops only after the same amount (or more)
233 ///\ref stop() "stop()"s. This can be useful e.g. to compute the running time
234 ///of recursive functions.
236 ///\todo This shouldn't be Unix (Linux) specific.
238 ///\author Alpar Juttner
241 int running; //Timer is running iff running>0; (running>=0 always holds)
242 TimeStamp start_time; //This is the relativ start-time if the timer
243 //is running, the collected running time otherwise.
245 void _reset() {if(running) start_time.stamp(); else start_time.reset();}
250 ///\param _running indicates whether or not the timer starts immediately.
252 Timer(bool _running=true) :running(_running) {_reset();}
254 ///Computes the ellapsed time
256 ///This conversion computes the ellapsed time
258 operator TimeStamp () const
262 return running?t-start_time:start_time;
265 ///Resets the time counters
267 ///Resets the time counters
274 ///Start the time counters
276 ///This function starts the time counters.
278 ///If the timer is started more than ones, it will remain running
279 ///until the same amount of \ref stop() is called.
283 if(running) running++;
287 start_time=t-start_time;
291 ///Stop the time counters
293 ///This function stops the time counters.
298 if(running && !--running) {
301 start_time=t-start_time;
305 ///Gives back the ellapsed user time of the process
306 double userTime() const
308 return operator TimeStamp().userTime();
310 ///Gives back the ellapsed system time of the process
311 double systemTime() const
313 return operator TimeStamp().systemTime();
315 ///Gives back the ellapsed user time of the process' children
316 double cUserTime() const
318 return operator TimeStamp().cUserTime();
320 ///Gives back the ellapsed user time of the process' children
321 double cSystemTime() const
323 return operator TimeStamp().cSystemTime();
325 ///Gives back the ellapsed real time
326 double realTime() const
328 return operator TimeStamp().realTime();
333 ///Prints the time counters
335 ///Prints the time counters in the following form:
337 /// <tt>u: XX.XXs s: XX.XXs cu: XX.XXs cs: XX.XXs real: XX.XXs</tt>
339 /// where the values are the
340 /// \li \c u: user cpu time,
341 /// \li \c s: system cpu time,
342 /// \li \c cu: user cpu time of children,
343 /// \li \c cs: system cpu time of children,
344 /// \li \c real: real time.
345 /// \relates TimeStamp
346 inline std::ostream& operator<<(std::ostream& os,const TimeStamp &t)
348 long cls = sysconf(_SC_CLK_TCK);
349 os << "u: " << double(t.getTms().tms_utime)/cls <<
350 "s, s: " << double(t.getTms().tms_stime)/cls <<
351 "s, cu: " << double(t.getTms().tms_cutime)/cls <<
352 "s, cs: " << double(t.getTms().tms_cstime)/cls <<
353 "s, real: " << t.realTime() << "s";
358 ///Tool to measure the running time more exactly.
360 ///This function calls \c f several times and returns the average
361 ///running time. The number of the executions will be choosen in such a way
362 ///that the full real running time will be roughly between \c min_time
363 ///and <tt>2*min_time</tt>.
364 ///\param f the function object to be measured.
365 ///\param min_time the minimum total running time.
366 ///\retval num if it is not \c NULL, then *num will contain the actual
367 /// number of execution of \c f.
368 ///\retval full_time if it is not \c NULL, then *full_time
369 /// will contain the actual
370 /// total running time.
371 ///\return The average running time of \c f.
374 TimeStamp runningTimeTest(F &f,double min_time=10,int *num = NULL,
375 TimeStamp *full_time=NULL)
380 for(int tn=1;tn < 1<<24; tn*=2) {
381 for(;total<tn;total++) f();
383 if(full.realTime()>min_time) {
385 if(full_time) *full_time=full;
397 #endif //LEMON_TIME_MEASURE_H