1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2009
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 #ifndef WIN32_LEAN_AND_MEAN
28 #define WIN32_LEAN_AND_MEAN
37 #include <sys/times.h>
47 /// \addtogroup timecount
50 /// A class to store (cpu)time instances.
52 /// This class stores five time values.
55 /// - a system cpu time
56 /// - a user cpu time of children
57 /// - a system cpu time of children
59 /// TimeStamp's can be added to or substracted from each other and
60 /// they can be pushed to a stream.
62 /// In most cases, perhaps the \ref Timer or the \ref TimeReport
63 /// class is what you want to use instead.
74 utime = stime = cutime = cstime = rtime = 0;
79 ///Read the current time values of the process
85 rtime=tv.tv_sec+double(tv.tv_usec)/1e6;
88 double tck=sysconf(_SC_CLK_TCK);
90 utime=ts.tms_utime/tck;
91 stime=ts.tms_stime/tck;
92 cutime=ts.tms_cutime/tck;
93 cstime=ts.tms_cstime/tck;
95 static const double ch = 4294967296.0e-7;
96 static const double cl = 1.0e-7;
99 GetSystemTimeAsFileTime(&system);
100 rtime = ch * system.dwHighDateTime + cl * system.dwLowDateTime;
102 FILETIME create, exit, kernel, user;
103 if (GetProcessTimes(GetCurrentProcess(),&create, &exit, &kernel, &user)) {
104 utime = ch * user.dwHighDateTime + cl * user.dwLowDateTime;
105 stime = ch * kernel.dwHighDateTime + cl * kernel.dwLowDateTime;
118 /// Constructor initializing with zero
121 ///Constructor initializing with the current time values of the process
122 TimeStamp(void *) { stamp();}
124 ///Set every time value to zero
125 TimeStamp &reset() {_reset();return *this;}
128 TimeStamp &operator+=(const TimeStamp &b)
138 TimeStamp operator+(const TimeStamp &b) const
144 TimeStamp &operator-=(const TimeStamp &b)
154 TimeStamp operator-(const TimeStamp &b) const
160 TimeStamp &operator*=(double b)
170 TimeStamp operator*(double b) const
175 friend TimeStamp operator*(double b,const TimeStamp &t);
177 TimeStamp &operator/=(double b)
187 TimeStamp operator/(double b) const
192 ///The time ellapsed since the last call of stamp()
193 TimeStamp ellapsed() const
199 friend std::ostream& operator<<(std::ostream& os,const TimeStamp &t);
201 ///Gives back the user time of the process
202 double userTime() const
206 ///Gives back the system time of the process
207 double systemTime() const
211 ///Gives back the user time of the process' children
213 ///\note On <tt>WIN32</tt> platform this value is not calculated.
215 double cUserTime() const
219 ///Gives back the user time of the process' children
221 ///\note On <tt>WIN32</tt> platform this value is not calculated.
223 double cSystemTime() const
227 ///Gives back the real time
228 double realTime() const {return rtime;}
231 TimeStamp operator*(double b,const TimeStamp &t)
236 ///Prints the time counters
238 ///Prints the time counters in the following form:
240 /// <tt>u: XX.XXs s: XX.XXs cu: XX.XXs cs: XX.XXs real: XX.XXs</tt>
242 /// where the values are the
243 /// \li \c u: user cpu time,
244 /// \li \c s: system cpu time,
245 /// \li \c cu: user cpu time of children,
246 /// \li \c cs: system cpu time of children,
247 /// \li \c real: real time.
248 /// \relates TimeStamp
249 /// \note On <tt>WIN32</tt> platform the cummulative values are not
251 inline std::ostream& operator<<(std::ostream& os,const TimeStamp &t)
253 os << "u: " << t.userTime() <<
254 "s, s: " << t.systemTime() <<
255 "s, cu: " << t.cUserTime() <<
256 "s, cs: " << t.cSystemTime() <<
257 "s, real: " << t.realTime() << "s";
261 ///Class for measuring the cpu time and real time usage of the process
263 ///Class for measuring the cpu time and real time usage of the process.
264 ///It is quite easy-to-use, here is a short example.
266 /// #include<lemon/time_measure.h>
267 /// #include<iostream>
276 /// std::cout << t << '\n';
278 /// doSomethingElse();
279 /// std::cout << t << '\n';
286 ///The \ref Timer can also be \ref stop() "stopped" and
287 ///\ref start() "started" again, so it is possible to compute collected
290 ///\warning Depending on the operation system and its actual configuration
291 ///the time counters have a certain (10ms on a typical Linux system)
293 ///Therefore this tool is not appropriate to measure very short times.
294 ///Also, if you start and stop the timer very frequently, it could lead to
295 ///distorted results.
297 ///\note If you want to measure the running time of the execution of a certain
298 ///function, consider the usage of \ref TimeReport instead.
303 int _running; //Timer is running iff _running>0; (_running>=0 always holds)
304 TimeStamp start_time; //This is the relativ start-time if the timer
305 //is _running, the collected _running time otherwise.
307 void _reset() {if(_running) start_time.stamp(); else start_time.reset();}
312 ///\param run indicates whether or not the timer starts immediately.
314 Timer(bool run=true) :_running(run) {_reset();}
316 ///\name Control the state of the timer
317 ///Basically a Timer can be either running or stopped,
318 ///but it provides a bit finer control on the execution.
319 ///The \ref lemon::Timer "Timer" also counts the number of
320 ///\ref lemon::Timer::start() "start()" executions, and it stops
321 ///only after the same amount (or more) \ref lemon::Timer::stop()
322 ///"stop()"s. This can be useful e.g. to compute the running time
323 ///of recursive functions.
327 ///Reset and stop the time counters
329 ///This function resets and stops the time counters
337 ///Start the time counters
339 ///This function starts the time counters.
341 ///If the timer is started more than ones, it will remain running
342 ///until the same amount of \ref stop() is called.
346 if(_running) _running++;
351 start_time=t-start_time;
356 ///Stop the time counters
358 ///This function stops the time counters. If start() was executed more than
359 ///once, then the same number of stop() execution is necessary the really
369 if(_running && !--_running) {
372 start_time=t-start_time;
376 ///Halt (i.e stop immediately) the time counters
378 ///This function stops immediately the time counters, i.e. <tt>t.halt()</tt>
380 ///equivalent of the following.
382 /// while(t.running()) t.stop()
396 start_time=t-start_time;
400 ///Returns the running state of the timer
402 ///This function returns the number of stop() exections that is
403 ///necessary to really stop the timer.
404 ///For example the timer
405 ///is running if and only if the return value is \c true
406 ///(i.e. greater than
408 int running() { return _running; }
411 ///Restart the time counters
413 ///This function is a shorthand for
414 ///a reset() and a start() calls.
424 ///\name Query Functions for the ellapsed time
428 ///Gives back the ellapsed user time of the process
429 double userTime() const
431 return operator TimeStamp().userTime();
433 ///Gives back the ellapsed system time of the process
434 double systemTime() const
436 return operator TimeStamp().systemTime();
438 ///Gives back the ellapsed user time of the process' children
440 ///\note On <tt>WIN32</tt> platform this value is not calculated.
442 double cUserTime() const
444 return operator TimeStamp().cUserTime();
446 ///Gives back the ellapsed user time of the process' children
448 ///\note On <tt>WIN32</tt> platform this value is not calculated.
450 double cSystemTime() const
452 return operator TimeStamp().cSystemTime();
454 ///Gives back the ellapsed real time
455 double realTime() const
457 return operator TimeStamp().realTime();
459 ///Computes the ellapsed time
461 ///This conversion computes the ellapsed time, therefore you can print
462 ///the ellapsed time like this.
466 /// std::cout << t << '\n';
468 operator TimeStamp () const
472 return _running?t-start_time:start_time;
479 ///Same as Timer but prints a report on destruction.
481 ///Same as \ref Timer but prints a report on destruction.
482 ///This example shows its usage.
484 /// void myAlg(ListGraph &g,int n)
486 /// TimeReport tr("Running time of myAlg: ");
487 /// ... //Here comes the algorithm
493 class TimeReport : public Timer
501 ///\param title This text will be printed before the ellapsed time.
502 ///\param os The stream to print the report to.
503 ///\param run Sets whether the timer should start immediately.
504 TimeReport(std::string title,std::ostream &os=std::cerr,bool run=true)
505 : Timer(run), _title(title), _os(os){}
506 ///Destructor that prints the ellapsed time
509 _os << _title << *this << std::endl;
513 ///'Do nothing' version of TimeReport
521 NoTimeReport(std::string,std::ostream &,bool) {}
523 NoTimeReport(std::string,std::ostream &) {}
525 NoTimeReport(std::string) {}
529 operator TimeStamp () const { return TimeStamp(); }
534 int running() { return 0; }
536 double userTime() const { return 0; }
537 double systemTime() const { return 0; }
538 double cUserTime() const { return 0; }
539 double cSystemTime() const { return 0; }
540 double realTime() const { return 0; }
543 ///Tool to measure the running time more exactly.
545 ///This function calls \c f several times and returns the average
546 ///running time. The number of the executions will be choosen in such a way
547 ///that the full real running time will be roughly between \c min_time
548 ///and <tt>2*min_time</tt>.
549 ///\param f the function object to be measured.
550 ///\param min_time the minimum total running time.
551 ///\retval num if it is not \c NULL, then the actual
552 /// number of execution of \c f will be written into <tt>*num</tt>.
553 ///\retval full_time if it is not \c NULL, then the actual
554 /// total running time will be written into <tt>*full_time</tt>.
555 ///\return The average running time of \c f.
558 TimeStamp runningTimeTest(F f,double min_time=10,unsigned int *num = NULL,
559 TimeStamp *full_time=NULL)
562 unsigned int total=0;
564 for(unsigned int tn=1;tn <= 1U<<31 && full.realTime()<=min_time; tn*=2) {
565 for(;total<tn;total++) f();
569 if(full_time) *full_time=full;
578 #endif //LEMON_TIME_MEASURE_H