Several doc improvements and fixes in maps.h and concepts/maps.h.
3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2008
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
28 #include <lemon/bits/mingw32_time.h>
30 #include <sys/times.h>
40 /// \addtogroup timecount
43 /// A class to store (cpu)time instances.
45 /// This class stores five time values.
48 /// - a system cpu time
49 /// - a user cpu time of children
50 /// - a system cpu time of children
52 /// TimeStamp's can be added to or substracted from each other and
53 /// they can be pushed to a stream.
55 /// In most cases, perhaps the \ref Timer or the \ref TimeReport
56 /// class is what you want to use instead.
58 ///\author Alpar Juttner
69 rtms(tms ts) : tms_utime(ts.tms_utime), tms_stime(ts.tms_stime),
70 tms_cutime(ts.tms_cutime), tms_cstime(ts.tms_cstime) {}
75 rtms &getTms() {return ts;}
76 const rtms &getTms() const {return ts;}
79 ts.tms_utime = ts.tms_stime = ts.tms_cutime = ts.tms_cstime = 0;
85 ///Read the current time values of the process
91 gettimeofday(&tv, 0);real_time=tv.tv_sec+double(tv.tv_usec)/1e6;
95 /// Constructor initializing with zero
98 ///Constructor initializing with the current time values of the process
99 TimeStamp(void *) { stamp();}
101 ///Set every time value to zero
102 TimeStamp &reset() {_reset();return *this;}
105 TimeStamp &operator+=(const TimeStamp &b)
107 ts.tms_utime+=b.ts.tms_utime;
108 ts.tms_stime+=b.ts.tms_stime;
109 ts.tms_cutime+=b.ts.tms_cutime;
110 ts.tms_cstime+=b.ts.tms_cstime;
111 real_time+=b.real_time;
115 TimeStamp operator+(const TimeStamp &b) const
121 TimeStamp &operator-=(const TimeStamp &b)
123 ts.tms_utime-=b.ts.tms_utime;
124 ts.tms_stime-=b.ts.tms_stime;
125 ts.tms_cutime-=b.ts.tms_cutime;
126 ts.tms_cstime-=b.ts.tms_cstime;
127 real_time-=b.real_time;
131 TimeStamp operator-(const TimeStamp &b) const
137 TimeStamp &operator*=(double b)
147 TimeStamp operator*(double b) const
152 friend TimeStamp operator*(double b,const TimeStamp &t);
154 TimeStamp &operator/=(double b)
164 TimeStamp operator/(double b) const
169 ///The time ellapsed since the last call of stamp()
170 TimeStamp ellapsed() const
176 friend std::ostream& operator<<(std::ostream& os,const TimeStamp &t);
178 ///Gives back the user time of the process
179 double userTime() const
181 return double(ts.tms_utime)/sysconf(_SC_CLK_TCK);
183 ///Gives back the system time of the process
184 double systemTime() const
186 return double(ts.tms_stime)/sysconf(_SC_CLK_TCK);
188 ///Gives back the user time of the process' children
189 double cUserTime() const
191 return double(ts.tms_cutime)/sysconf(_SC_CLK_TCK);
193 ///Gives back the user time of the process' children
194 double cSystemTime() const
196 return double(ts.tms_cstime)/sysconf(_SC_CLK_TCK);
198 ///Gives back the real time
199 double realTime() const {return real_time;}
202 TimeStamp operator*(double b,const TimeStamp &t)
207 ///Prints the time counters
209 ///Prints the time counters in the following form:
211 /// <tt>u: XX.XXs s: XX.XXs cu: XX.XXs cs: XX.XXs real: XX.XXs</tt>
213 /// where the values are the
214 /// \li \c u: user cpu time,
215 /// \li \c s: system cpu time,
216 /// \li \c cu: user cpu time of children,
217 /// \li \c cs: system cpu time of children,
218 /// \li \c real: real time.
219 /// \relates TimeStamp
220 inline std::ostream& operator<<(std::ostream& os,const TimeStamp &t)
222 long cls = sysconf(_SC_CLK_TCK);
223 os << "u: " << double(t.getTms().tms_utime)/cls <<
224 "s, s: " << double(t.getTms().tms_stime)/cls <<
225 "s, cu: " << double(t.getTms().tms_cutime)/cls <<
226 "s, cs: " << double(t.getTms().tms_cstime)/cls <<
227 "s, real: " << t.realTime() << "s";
231 ///Class for measuring the cpu time and real time usage of the process
233 ///Class for measuring the cpu time and real time usage of the process.
234 ///It is quite easy-to-use, here is a short example.
236 /// #include<lemon/time_measure.h>
237 /// #include<iostream>
246 /// std::cout << t << '\n';
248 /// doSomethingElse();
249 /// std::cout << t << '\n';
256 ///The \ref Timer can also be \ref stop() "stopped" and
257 ///\ref start() "started" again, so it is possible to compute collected
260 ///\warning Depending on the operation system and its actual configuration
261 ///the time counters have a certain (10ms on a typical Linux system)
263 ///Therefore this tool is not appropriate to measure very short times.
264 ///Also, if you start and stop the timer very frequently, it could lead to
265 ///distorted results.
267 ///\note If you want to measure the running time of the execution of a certain
268 ///function, consider the usage of \ref TimeReport instead.
270 ///\todo This shouldn't be Unix (Linux) specific.
273 ///\author Alpar Juttner
276 int _running; //Timer is running iff _running>0; (_running>=0 always holds)
277 TimeStamp start_time; //This is the relativ start-time if the timer
278 //is _running, the collected _running time otherwise.
280 void _reset() {if(_running) start_time.stamp(); else start_time.reset();}
285 ///\param run indicates whether or not the timer starts immediately.
287 Timer(bool run=true) :_running(run) {_reset();}
289 ///\name Control the state of the timer
290 ///Basically a Timer can be either running or stopped,
291 ///but it provides a bit finer control on the execution.
292 ///The \ref Timer also counts the number of \ref start()
293 ///executions, and is stops only after the same amount (or more)
294 ///\ref stop() "stop()"s. This can be useful e.g. to compute the running time
295 ///of recursive functions.
300 ///Reset and stop the time counters
302 ///This function resets and stops the time counters
310 ///Start the time counters
312 ///This function starts the time counters.
314 ///If the timer is started more than ones, it will remain running
315 ///until the same amount of \ref stop() is called.
319 if(_running) _running++;
324 start_time=t-start_time;
329 ///Stop the time counters
331 ///This function stops the time counters. If start() was executed more than
332 ///once, then the same number of stop() execution is necessary the really
342 if(_running && !--_running) {
345 start_time=t-start_time;
349 ///Halt (i.e stop immediately) the time counters
351 ///This function stops immediately the time counters, i.e. <tt>t.stop()</tt>
353 ///equivalent of the following.
355 /// while(t.running()) t.stop()
369 start_time=t-start_time;
373 ///Returns the running state of the timer
375 ///This function returns the number of stop() exections that is
376 ///necessary to really stop the timer.
377 ///For example the timer
378 ///is running if and only if the return value is \c true
379 ///(i.e. greater than
381 int running() { return _running; }
384 ///Restart the time counters
386 ///This function is a shorthand for
387 ///a reset() and a start() calls.
397 ///\name Query Functions for the ellapsed time
401 ///Gives back the ellapsed user time of the process
402 double userTime() const
404 return operator TimeStamp().userTime();
406 ///Gives back the ellapsed system time of the process
407 double systemTime() const
409 return operator TimeStamp().systemTime();
411 ///Gives back the ellapsed user time of the process' children
412 double cUserTime() const
414 return operator TimeStamp().cUserTime();
416 ///Gives back the ellapsed user time of the process' children
417 double cSystemTime() const
419 return operator TimeStamp().cSystemTime();
421 ///Gives back the ellapsed real time
422 double realTime() const
424 return operator TimeStamp().realTime();
426 ///Computes the ellapsed time
428 ///This conversion computes the ellapsed time, therefore you can print
429 ///the ellapsed time like this.
433 /// std::cout << t << '\n';
435 operator TimeStamp () const
439 return _running?t-start_time:start_time;
446 ///Same as \ref Timer but prints a report on destruction.
448 ///Same as \ref Timer but prints a report on destruction.
449 ///This example shows its usage.
451 /// void myAlg(ListGraph &g,int n)
453 /// TimeReport tr("Running time of myAlg: ");
454 /// ... //Here comes the algorithm
460 ///\todo There is no test case for this
461 class TimeReport : public Timer
468 ///\param title This text will be printed before the ellapsed time.
469 ///\param os The stream to print the report to.
470 ///\param run Sets whether the timer should start immediately.
472 TimeReport(std::string title,std::ostream &os=std::cerr,bool run=true)
473 : Timer(run), _title(title), _os(os){}
474 ///\e Prints the ellapsed time on destruction.
477 _os << _title << *this << std::endl;
481 ///'Do nothing' version of \ref TimeReport
489 NoTimeReport(std::string,std::ostream &,bool) {}
491 NoTimeReport(std::string,std::ostream &) {}
493 NoTimeReport(std::string) {}
497 operator TimeStamp () const { return TimeStamp(); }
502 int running() { return 0; }
504 double userTime() const { return 0; }
505 double systemTime() const { return 0; }
506 double cUserTime() const { return 0; }
507 double cSystemTime() const { return 0; }
508 double realTime() const { return 0; }
511 ///Tool to measure the running time more exactly.
513 ///This function calls \c f several times and returns the average
514 ///running time. The number of the executions will be choosen in such a way
515 ///that the full real running time will be roughly between \c min_time
516 ///and <tt>2*min_time</tt>.
517 ///\param f the function object to be measured.
518 ///\param min_time the minimum total running time.
519 ///\retval num if it is not \c NULL, then the actual
520 /// number of execution of \c f will be written into <tt>*num</tt>.
521 ///\retval full_time if it is not \c NULL, then the actual
522 /// total running time will be written into <tt>*full_time</tt>.
523 ///\return The average running time of \c f.
526 TimeStamp runningTimeTest(F f,double min_time=10,unsigned int *num = NULL,
527 TimeStamp *full_time=NULL)
530 unsigned int total=0;
532 for(unsigned int tn=1;tn <= 1U<<31 && full.realTime()<=min_time; tn*=2) {
533 for(;total<tn;total++) f();
537 if(full_time) *full_time=full;
546 #endif //LEMON_TIME_MEASURE_H