Fight with Doxygen.
Victory hasn't been reached yet, but it's on the horizon.
2 * lemon/time_measure.h - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2006 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 the \ref Timer or the \ref TimeReport
48 /// class is what you want to use instead.
50 ///\author Alpar Juttner
61 rtms(tms ts) : tms_utime(ts.tms_utime), tms_stime(ts.tms_stime),
62 tms_cutime(ts.tms_cutime), tms_cstime(ts.tms_cstime) {}
67 rtms &getTms() {return ts;}
68 const rtms &getTms() const {return ts;}
71 { ts.tms_utime=ts.tms_stime=ts.tms_cutime=ts.tms_cstime=0; real_time=0;}
75 ///Read the current time values of the process
81 gettimeofday(&tv, 0);real_time=tv.tv_sec+double(tv.tv_usec)/1e6;
85 /// Constructor initializing with zero
88 ///Constructor initializing with the current time values of the process
89 TimeStamp(void *) { stamp();}
91 ///Set every time value to zero
92 TimeStamp &reset() {_reset();return *this;}
95 TimeStamp &operator+=(const TimeStamp &b)
97 ts.tms_utime+=b.ts.tms_utime;
98 ts.tms_stime+=b.ts.tms_stime;
99 ts.tms_cutime+=b.ts.tms_cutime;
100 ts.tms_cstime+=b.ts.tms_cstime;
101 real_time+=b.real_time;
105 TimeStamp operator+(const TimeStamp &b) const
111 TimeStamp &operator-=(const TimeStamp &b)
113 ts.tms_utime-=b.ts.tms_utime;
114 ts.tms_stime-=b.ts.tms_stime;
115 ts.tms_cutime-=b.ts.tms_cutime;
116 ts.tms_cstime-=b.ts.tms_cstime;
117 real_time-=b.real_time;
121 TimeStamp operator-(const TimeStamp &b) const
127 TimeStamp &operator*=(double b)
137 TimeStamp operator*(double b) const
142 friend TimeStamp operator*(double b,const TimeStamp &t);
144 TimeStamp &operator/=(double b)
154 TimeStamp operator/(double b) const
159 ///The time ellapsed since the last call of stamp()
160 TimeStamp ellapsed() const
166 friend std::ostream& operator<<(std::ostream& os,const TimeStamp &t);
168 ///Gives back the user time of the process
169 double userTime() const
171 return double(ts.tms_utime)/sysconf(_SC_CLK_TCK);
173 ///Gives back the system time of the process
174 double systemTime() const
176 return double(ts.tms_stime)/sysconf(_SC_CLK_TCK);
178 ///Gives back the user time of the process' children
179 double cUserTime() const
181 return double(ts.tms_cutime)/sysconf(_SC_CLK_TCK);
183 ///Gives back the user time of the process' children
184 double cSystemTime() const
186 return double(ts.tms_cstime)/sysconf(_SC_CLK_TCK);
188 ///Gives back the real time
189 double realTime() const {return real_time;}
192 TimeStamp operator*(double b,const TimeStamp &t)
197 ///Prints the time counters
199 ///Prints the time counters in the following form:
201 /// <tt>u: XX.XXs s: XX.XXs cu: XX.XXs cs: XX.XXs real: XX.XXs</tt>
203 /// where the values are the
204 /// \li \c u: user cpu time,
205 /// \li \c s: system cpu time,
206 /// \li \c cu: user cpu time of children,
207 /// \li \c cs: system cpu time of children,
208 /// \li \c real: real time.
209 /// \relates TimeStamp
210 inline std::ostream& operator<<(std::ostream& os,const TimeStamp &t)
212 long cls = sysconf(_SC_CLK_TCK);
213 os << "u: " << double(t.getTms().tms_utime)/cls <<
214 "s, s: " << double(t.getTms().tms_stime)/cls <<
215 "s, cu: " << double(t.getTms().tms_cutime)/cls <<
216 "s, cs: " << double(t.getTms().tms_cstime)/cls <<
217 "s, real: " << t.realTime() << "s";
221 ///Class for measuring the cpu time and real time usage of the process
223 ///Class for measuring the cpu time and real time usage of the process.
224 ///It is quite easy-to-use, here is a short example.
226 ///#include<lemon/time_measure.h>
227 ///#include<iostream>
236 /// std::cout << T << '\n';
238 /// doSomethingElse();
239 /// std::cout << T << '\n';
246 ///The \ref Timer can also be \ref stop() "stopped" and
247 ///\ref start() "started" again, so it is possible to compute collected
250 ///\warning Depending on the operation system and its actual configuration
251 ///the time counters have a certain (10ms on a typical Linux system)
253 ///Therefore this tool is not appropriate to measure very short times.
254 ///Also, if you start and stop the timer very frequently, it could lead
255 ///distorted results.
257 ///\note If you want to measure the running time of the execution of a certain
258 ///function, consider the usage of \ref TimeReport instead.
260 ///\todo This shouldn't be Unix (Linux) specific.
263 ///\author Alpar Juttner
266 int _running; //Timer is running iff _running>0; (_running>=0 always holds)
267 TimeStamp start_time; //This is the relativ start-time if the timer
268 //is _running, the collected _running time otherwise.
270 void _reset() {if(_running) start_time.stamp(); else start_time.reset();}
275 ///\param _running indicates whether or not the timer starts immediately.
277 Timer(bool run=true) :_running(run) {_reset();}
279 ///\name Control the state of the timer
280 ///Basically a Timer can be either running or stopped,
281 ///but it provides a bit finer control on the execution.
282 ///The \ref Timer also counts the number of \ref start()
283 ///executions, and is stops only after the same amount (or more)
284 ///\ref stop() "stop()"s. This can be useful e.g. to compute the running time
285 ///of recursive functions.
290 ///Reset and stop the time counters
292 ///This function resets and stops the time counters
300 ///Start the time counters
302 ///This function starts the time counters.
304 ///If the timer is started more than ones, it will remain running
305 ///until the same amount of \ref stop() is called.
309 if(_running) _running++;
314 start_time=t-start_time;
319 ///Stop the time counters
321 ///This function stops the time counters. If start() was executed more than
322 ///once, then the same number of stop() execution is necessary the really
332 if(_running && !--_running) {
335 start_time=t-start_time;
339 ///Halt (i.e stop immediately) the time counters
341 ///This function stops immediately the time counters.
353 start_time=t-start_time;
357 ///Returns the running state of the timer
359 ///This function returns the number of stop() exections that is
360 ///necessary to really stop the timer.
361 ///For example the timer
362 ///is running if and only if the return value is \c true
363 ///(i.e. greater than
365 int running() { return _running; }
368 ///Restart the time counters
370 ///This function is a shorthand for
371 ///a reset() and a start() calls.
381 ///\name Query Functions for the ellapsed time
385 ///Gives back the ellapsed user time of the process
386 double userTime() const
388 return operator TimeStamp().userTime();
390 ///Gives back the ellapsed system time of the process
391 double systemTime() const
393 return operator TimeStamp().systemTime();
395 ///Gives back the ellapsed user time of the process' children
396 double cUserTime() const
398 return operator TimeStamp().cUserTime();
400 ///Gives back the ellapsed user time of the process' children
401 double cSystemTime() const
403 return operator TimeStamp().cSystemTime();
405 ///Gives back the ellapsed real time
406 double realTime() const
408 return operator TimeStamp().realTime();
410 ///Computes the ellapsed time
412 ///This conversion computes the ellapsed time, therefore you can print
413 ///the ellapsed time like this.
417 /// std::cout << T << '\n';
419 operator TimeStamp () const
423 return _running?t-start_time:start_time;
430 ///Same as \ref Timer but prints a report on destruction.
432 ///Same as \ref Timer but prints a report on destruction.
433 ///This example shows its usage.
435 /// void myAlg(ListGraph &g,int n)
437 /// TimeReport TR("Running time of myAlg: ");
438 /// ... //Here comes the algorithm
444 ///\todo There is no test case for this
445 class TimeReport : public Timer
452 ///\param title This text will be printed before the ellapsed time.
453 ///\param os The stream to print the report to.
454 ///\param run Sets whether the timer should start immediately.
456 TimeReport(std::string title,std::ostream &os=std::cerr,bool run=true)
457 : Timer(run), _title(title), _os(os){}
458 ///\e Prints the ellapsed time on destruction.
461 _os << _title << *this << std::endl;
465 ///'Do nothing' version of \ref TimeReport
473 NoTimeReport(std::string,std::ostream &,bool) {}
475 NoTimeReport(std::string,std::ostream &) {}
477 NoTimeReport(std::string) {}
481 operator TimeStamp () const { return TimeStamp(); }
486 int running() { return 0; }
488 double userTime() const { return 0; }
489 double systemTime() const { return 0; }
490 double cUserTime() const { return 0; }
491 double cSystemTime() const { return 0; }
492 double realTime() const { return 0; }
495 ///Tool to measure the running time more exactly.
497 ///This function calls \c f several times and returns the average
498 ///running time. The number of the executions will be choosen in such a way
499 ///that the full real running time will be roughly between \c min_time
500 ///and <tt>2*min_time</tt>.
501 ///\param f the function object to be measured.
502 ///\param min_time the minimum total running time.
503 ///\retval num if it is not \c NULL, then the actual
504 /// number of execution of \c f will be written into <tt>*num</tt>.
505 ///\retval full_time if it is not \c NULL, then the actual
506 /// total running time will be written into <tt>*full_time</tt>.
507 ///\return The average running time of \c f.
510 TimeStamp runningTimeTest(F f,double min_time=10,int *num = NULL,
511 TimeStamp *full_time=NULL)
516 for(int tn=1;tn < 1<<24; tn*=2) {
517 for(;total<tn;total++) f();
519 if(full.realTime()>min_time) {
521 if(full_time) *full_time=full;
533 #endif //LEMON_TIME_MEASURE_H