Unionfind changes induced some bugs here. Also some augmentations made.
3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2006
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
19 #ifndef LEMON_TIME_MEASURE_H
20 #define LEMON_TIME_MEASURE_H
24 ///\brief Tools for measuring cpu usage
27 #include <sys/times.h>
34 /// \addtogroup timecount
37 /// A class to store (cpu)time instances.
39 /// This class stores five time values.
42 /// - a system cpu time
43 /// - a user cpu time of children
44 /// - a system cpu time of children
46 /// TimeStamp's can be added to or substracted from each other and
47 /// they can be pushed to a stream.
49 /// In most cases, perhaps the \ref Timer or the \ref TimeReport
50 /// class is what you want to use instead.
52 ///\author Alpar Juttner
63 rtms(tms ts) : tms_utime(ts.tms_utime), tms_stime(ts.tms_stime),
64 tms_cutime(ts.tms_cutime), tms_cstime(ts.tms_cstime) {}
69 rtms &getTms() {return ts;}
70 const rtms &getTms() const {return ts;}
73 { ts.tms_utime=ts.tms_stime=ts.tms_cutime=ts.tms_cstime=0; real_time=0;}
77 ///Read the current time values of the process
83 gettimeofday(&tv, 0);real_time=tv.tv_sec+double(tv.tv_usec)/1e6;
87 /// Constructor initializing with zero
90 ///Constructor initializing with the current time values of the process
91 TimeStamp(void *) { stamp();}
93 ///Set every time value to zero
94 TimeStamp &reset() {_reset();return *this;}
97 TimeStamp &operator+=(const TimeStamp &b)
99 ts.tms_utime+=b.ts.tms_utime;
100 ts.tms_stime+=b.ts.tms_stime;
101 ts.tms_cutime+=b.ts.tms_cutime;
102 ts.tms_cstime+=b.ts.tms_cstime;
103 real_time+=b.real_time;
107 TimeStamp operator+(const TimeStamp &b) const
113 TimeStamp &operator-=(const TimeStamp &b)
115 ts.tms_utime-=b.ts.tms_utime;
116 ts.tms_stime-=b.ts.tms_stime;
117 ts.tms_cutime-=b.ts.tms_cutime;
118 ts.tms_cstime-=b.ts.tms_cstime;
119 real_time-=b.real_time;
123 TimeStamp operator-(const TimeStamp &b) const
129 TimeStamp &operator*=(double b)
139 TimeStamp operator*(double b) const
144 friend TimeStamp operator*(double b,const TimeStamp &t);
146 TimeStamp &operator/=(double b)
156 TimeStamp operator/(double b) const
161 ///The time ellapsed since the last call of stamp()
162 TimeStamp ellapsed() const
168 friend std::ostream& operator<<(std::ostream& os,const TimeStamp &t);
170 ///Gives back the user time of the process
171 double userTime() const
173 return double(ts.tms_utime)/sysconf(_SC_CLK_TCK);
175 ///Gives back the system time of the process
176 double systemTime() const
178 return double(ts.tms_stime)/sysconf(_SC_CLK_TCK);
180 ///Gives back the user time of the process' children
181 double cUserTime() const
183 return double(ts.tms_cutime)/sysconf(_SC_CLK_TCK);
185 ///Gives back the user time of the process' children
186 double cSystemTime() const
188 return double(ts.tms_cstime)/sysconf(_SC_CLK_TCK);
190 ///Gives back the real time
191 double realTime() const {return real_time;}
194 TimeStamp operator*(double b,const TimeStamp &t)
199 ///Prints the time counters
201 ///Prints the time counters in the following form:
203 /// <tt>u: XX.XXs s: XX.XXs cu: XX.XXs cs: XX.XXs real: XX.XXs</tt>
205 /// where the values are the
206 /// \li \c u: user cpu time,
207 /// \li \c s: system cpu time,
208 /// \li \c cu: user cpu time of children,
209 /// \li \c cs: system cpu time of children,
210 /// \li \c real: real time.
211 /// \relates TimeStamp
212 inline std::ostream& operator<<(std::ostream& os,const TimeStamp &t)
214 long cls = sysconf(_SC_CLK_TCK);
215 os << "u: " << double(t.getTms().tms_utime)/cls <<
216 "s, s: " << double(t.getTms().tms_stime)/cls <<
217 "s, cu: " << double(t.getTms().tms_cutime)/cls <<
218 "s, cs: " << double(t.getTms().tms_cstime)/cls <<
219 "s, real: " << t.realTime() << "s";
223 ///Class for measuring the cpu time and real time usage of the process
225 ///Class for measuring the cpu time and real time usage of the process.
226 ///It is quite easy-to-use, here is a short example.
228 ///#include<lemon/time_measure.h>
229 ///#include<iostream>
238 /// std::cout << T << '\n';
240 /// doSomethingElse();
241 /// std::cout << T << '\n';
248 ///The \ref Timer can also be \ref stop() "stopped" and
249 ///\ref start() "started" again, so it is possible to compute collected
252 ///\warning Depending on the operation system and its actual configuration
253 ///the time counters have a certain (10ms on a typical Linux system)
255 ///Therefore this tool is not appropriate to measure very short times.
256 ///Also, if you start and stop the timer very frequently, it could lead
257 ///distorted results.
259 ///\note If you want to measure the running time of the execution of a certain
260 ///function, consider the usage of \ref TimeReport instead.
262 ///\todo This shouldn't be Unix (Linux) specific.
265 ///\author Alpar Juttner
268 int _running; //Timer is running iff _running>0; (_running>=0 always holds)
269 TimeStamp start_time; //This is the relativ start-time if the timer
270 //is _running, the collected _running time otherwise.
272 void _reset() {if(_running) start_time.stamp(); else start_time.reset();}
277 ///\param run indicates whether or not the timer starts immediately.
279 Timer(bool run=true) :_running(run) {_reset();}
281 ///\name Control the state of the timer
282 ///Basically a Timer can be either running or stopped,
283 ///but it provides a bit finer control on the execution.
284 ///The \ref Timer also counts the number of \ref start()
285 ///executions, and is stops only after the same amount (or more)
286 ///\ref stop() "stop()"s. This can be useful e.g. to compute the running time
287 ///of recursive functions.
292 ///Reset and stop the time counters
294 ///This function resets and stops the time counters
302 ///Start the time counters
304 ///This function starts the time counters.
306 ///If the timer is started more than ones, it will remain running
307 ///until the same amount of \ref stop() is called.
311 if(_running) _running++;
316 start_time=t-start_time;
321 ///Stop the time counters
323 ///This function stops the time counters. If start() was executed more than
324 ///once, then the same number of stop() execution is necessary the really
334 if(_running && !--_running) {
337 start_time=t-start_time;
341 ///Halt (i.e stop immediately) the time counters
343 ///This function stops immediately the time counters.
355 start_time=t-start_time;
359 ///Returns the running state of the timer
361 ///This function returns the number of stop() exections that is
362 ///necessary to really stop the timer.
363 ///For example the timer
364 ///is running if and only if the return value is \c true
365 ///(i.e. greater than
367 int running() { return _running; }
370 ///Restart the time counters
372 ///This function is a shorthand for
373 ///a reset() and a start() calls.
383 ///\name Query Functions for the ellapsed time
387 ///Gives back the ellapsed user time of the process
388 double userTime() const
390 return operator TimeStamp().userTime();
392 ///Gives back the ellapsed system time of the process
393 double systemTime() const
395 return operator TimeStamp().systemTime();
397 ///Gives back the ellapsed user time of the process' children
398 double cUserTime() const
400 return operator TimeStamp().cUserTime();
402 ///Gives back the ellapsed user time of the process' children
403 double cSystemTime() const
405 return operator TimeStamp().cSystemTime();
407 ///Gives back the ellapsed real time
408 double realTime() const
410 return operator TimeStamp().realTime();
412 ///Computes the ellapsed time
414 ///This conversion computes the ellapsed time, therefore you can print
415 ///the ellapsed time like this.
419 /// std::cout << T << '\n';
421 operator TimeStamp () const
425 return _running?t-start_time:start_time;
432 ///Same as \ref Timer but prints a report on destruction.
434 ///Same as \ref Timer but prints a report on destruction.
435 ///This example shows its usage.
437 /// void myAlg(ListGraph &g,int n)
439 /// TimeReport TR("Running time of myAlg: ");
440 /// ... //Here comes the algorithm
446 ///\todo There is no test case for this
447 class TimeReport : public Timer
454 ///\param title This text will be printed before the ellapsed time.
455 ///\param os The stream to print the report to.
456 ///\param run Sets whether the timer should start immediately.
458 TimeReport(std::string title,std::ostream &os=std::cerr,bool run=true)
459 : Timer(run), _title(title), _os(os){}
460 ///\e Prints the ellapsed time on destruction.
463 _os << _title << *this << std::endl;
467 ///'Do nothing' version of \ref TimeReport
475 NoTimeReport(std::string,std::ostream &,bool) {}
477 NoTimeReport(std::string,std::ostream &) {}
479 NoTimeReport(std::string) {}
483 operator TimeStamp () const { return TimeStamp(); }
488 int running() { return 0; }
490 double userTime() const { return 0; }
491 double systemTime() const { return 0; }
492 double cUserTime() const { return 0; }
493 double cSystemTime() const { return 0; }
494 double realTime() const { return 0; }
497 ///Tool to measure the running time more exactly.
499 ///This function calls \c f several times and returns the average
500 ///running time. The number of the executions will be choosen in such a way
501 ///that the full real running time will be roughly between \c min_time
502 ///and <tt>2*min_time</tt>.
503 ///\param f the function object to be measured.
504 ///\param min_time the minimum total running time.
505 ///\retval num if it is not \c NULL, then the actual
506 /// number of execution of \c f will be written into <tt>*num</tt>.
507 ///\retval full_time if it is not \c NULL, then the actual
508 /// total running time will be written into <tt>*full_time</tt>.
509 ///\return The average running time of \c f.
512 TimeStamp runningTimeTest(F f,double min_time=10,int *num = NULL,
513 TimeStamp *full_time=NULL)
518 for(int tn=1;tn < 1<<30; tn*=2) {
519 for(;total<tn;total++) f();
521 if(full.realTime()>min_time) {
523 if(full_time) *full_time=full;
535 #endif //LEMON_TIME_MEASURE_H