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>
32 /// \addtogroup timecount
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 possible to compute collected
225 ///\warning Depending on the operation system and its actual configuration
226 ///the time counters have a certain (10ms on a typical Linux system)
228 ///Therefore this tool is not appropriate to measure very short times.
229 ///Also, if you start and stop the timer very frequently, it could lead
230 ///distorted results.
232 ///The \ref Timer also counts the number of \ref start()
233 ///executions, and is stops only after the same amount (or more)
234 ///\ref stop() "stop()"s. This can be useful e.g. to compute the running time
235 ///of recursive functions.
237 ///\todo This shouldn't be Unix (Linux) specific.
239 ///\author Alpar Juttner
242 int _running; //Timer is running iff _running>0; (_running>=0 always holds)
243 TimeStamp start_time; //This is the relativ start-time if the timer
244 //is _running, the collected _running time otherwise.
246 void _reset() {if(_running) start_time.stamp(); else start_time.reset();}
251 ///\param _running indicates whether or not the timer starts immediately.
253 Timer(bool run=true) :_running(run) {_reset();}
255 ///Computes the ellapsed time
257 ///This conversion computes the ellapsed time
259 operator TimeStamp () const
263 return _running?t-start_time:start_time;
266 ///Reset and stop the time counters
268 ///This function resets and stops the time counters
276 ///Start the time counters
278 ///This function starts the time counters.
280 ///If the timer is started more than ones, it will remain running
281 ///until the same amount of \ref stop() is called.
285 if(_running) _running++;
289 start_time=t-start_time;
294 ///Stop the time counters
296 ///This function stops the time counters. If start() was executed more than
297 ///once, then the same number of stop() execution is necessary the really
307 if(_running && !--_running) {
310 start_time=t-start_time;
314 ///Halt (i.e stop immediately) the time counters
316 ///This function stops immediately the time counters.
328 start_time=t-start_time;
332 ///Returns the running state of the timer
334 ///This function returns the number of stop() exections that is
335 ///necessary to really stop the timer.
336 ///For example the timer
337 ///is running if and only if the return value is \c true
338 ///(i.e. greater than
340 int running() { return _running; }
343 ///Restart the time counters
345 ///This function is a shorthand for
346 ///a reset() and a start() calls.
354 ///Gives back the ellapsed user time of the process
355 double userTime() const
357 return operator TimeStamp().userTime();
359 ///Gives back the ellapsed system time of the process
360 double systemTime() const
362 return operator TimeStamp().systemTime();
364 ///Gives back the ellapsed user time of the process' children
365 double cUserTime() const
367 return operator TimeStamp().cUserTime();
369 ///Gives back the ellapsed user time of the process' children
370 double cSystemTime() const
372 return operator TimeStamp().cSystemTime();
374 ///Gives back the ellapsed real time
375 double realTime() const
377 return operator TimeStamp().realTime();
382 ///Same as \ref Timer but prints a report on destruction.
384 ///Same as \ref Timer but prints a report on destruction.
386 class TimeReport : public Timer
393 TimeReport(std::string title,std::ostream &os,bool run)
394 : Timer(run), _title(title), _os(os){}
397 _os << _title << this << std::endl;
401 ///Prints the time counters
403 ///Prints the time counters in the following form:
405 /// <tt>u: XX.XXs s: XX.XXs cu: XX.XXs cs: XX.XXs real: XX.XXs</tt>
407 /// where the values are the
408 /// \li \c u: user cpu time,
409 /// \li \c s: system cpu time,
410 /// \li \c cu: user cpu time of children,
411 /// \li \c cs: system cpu time of children,
412 /// \li \c real: real time.
413 /// \relates TimeStamp
414 inline std::ostream& operator<<(std::ostream& os,const TimeStamp &t)
416 long cls = sysconf(_SC_CLK_TCK);
417 os << "u: " << double(t.getTms().tms_utime)/cls <<
418 "s, s: " << double(t.getTms().tms_stime)/cls <<
419 "s, cu: " << double(t.getTms().tms_cutime)/cls <<
420 "s, cs: " << double(t.getTms().tms_cstime)/cls <<
421 "s, real: " << t.realTime() << "s";
426 ///Tool to measure the running time more exactly.
428 ///This function calls \c f several times and returns the average
429 ///running time. The number of the executions will be choosen in such a way
430 ///that the full real running time will be roughly between \c min_time
431 ///and <tt>2*min_time</tt>.
432 ///\param f the function object to be measured.
433 ///\param min_time the minimum total running time.
434 ///\retval num if it is not \c NULL, then *num will contain the actual
435 /// number of execution of \c f.
436 ///\retval full_time if it is not \c NULL, then *full_time
437 /// will contain the actual
438 /// total running time.
439 ///\return The average running time of \c f.
442 TimeStamp runningTimeTest(F f,double min_time=10,int *num = NULL,
443 TimeStamp *full_time=NULL)
448 for(int tn=1;tn < 1<<24; tn*=2) {
449 for(;total<tn;total++) f();
451 if(full.realTime()>min_time) {
453 if(full_time) *full_time=full;
465 #endif //LEMON_TIME_MEASURE_H