1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
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
27 #define WIN32_LEAN_AND_MEAN
33 #include <sys/times.h>
43 /// \addtogroup timecount
46 /// A class to store (cpu)time instances.
48 /// This class stores five time values.
51 /// - a system cpu time
52 /// - a user cpu time of children
53 /// - a system cpu time of children
55 /// TimeStamp's can be added to or substracted from each other and
56 /// they can be pushed to a stream.
58 /// In most cases, perhaps the \ref Timer or the \ref TimeReport
59 /// class is what you want to use instead.
70 utime = stime = cutime = cstime = rtime = 0;
75 ///Read the current time values of the process
81 rtime=tv.tv_sec+double(tv.tv_usec)/1e6;
84 double tck=sysconf(_SC_CLK_TCK);
86 utime=ts.tms_utime/tck;
87 stime=ts.tms_stime/tck;
88 cutime=ts.tms_cutime/tck;
89 cstime=ts.tms_cstime/tck;
91 static const double ch = 4294967296.0e-7;
92 static const double cl = 1.0e-7;
95 GetSystemTimeAsFileTime(&system);
96 rtime = ch * system.dwHighDateTime + cl * system.dwLowDateTime;
98 FILETIME create, exit, kernel, user;
99 if (GetProcessTimes(GetCurrentProcess(),&create, &exit, &kernel, &user)) {
100 utime = ch * user.dwHighDateTime + cl * user.dwLowDateTime;
101 stime = ch * kernel.dwHighDateTime + cl * kernel.dwLowDateTime;
114 /// Constructor initializing with zero
117 ///Constructor initializing with the current time values of the process
118 TimeStamp(void *) { stamp();}
120 ///Set every time value to zero
121 TimeStamp &reset() {_reset();return *this;}
124 TimeStamp &operator+=(const TimeStamp &b)
134 TimeStamp operator+(const TimeStamp &b) const
140 TimeStamp &operator-=(const TimeStamp &b)
150 TimeStamp operator-(const TimeStamp &b) const
156 TimeStamp &operator*=(double b)
166 TimeStamp operator*(double b) const
171 friend TimeStamp operator*(double b,const TimeStamp &t);
173 TimeStamp &operator/=(double b)
183 TimeStamp operator/(double b) const
188 ///The time ellapsed since the last call of stamp()
189 TimeStamp ellapsed() const
195 friend std::ostream& operator<<(std::ostream& os,const TimeStamp &t);
197 ///Gives back the user time of the process
198 double userTime() const
202 ///Gives back the system time of the process
203 double systemTime() const
207 ///Gives back the user time of the process' children
209 ///\note On <tt>WIN32</tt> platform this value is not calculated.
211 double cUserTime() const
215 ///Gives back the user time of the process' children
217 ///\note On <tt>WIN32</tt> platform this value is not calculated.
219 double cSystemTime() const
223 ///Gives back the real time
224 double realTime() const {return rtime;}
227 TimeStamp operator*(double b,const TimeStamp &t)
232 ///Prints the time counters
234 ///Prints the time counters in the following form:
236 /// <tt>u: XX.XXs s: XX.XXs cu: XX.XXs cs: XX.XXs real: XX.XXs</tt>
238 /// where the values are the
239 /// \li \c u: user cpu time,
240 /// \li \c s: system cpu time,
241 /// \li \c cu: user cpu time of children,
242 /// \li \c cs: system cpu time of children,
243 /// \li \c real: real time.
244 /// \relates TimeStamp
245 /// \note On <tt>WIN32</tt> platform the cummulative values are not
247 inline std::ostream& operator<<(std::ostream& os,const TimeStamp &t)
249 os << "u: " << t.userTime() <<
250 "s, s: " << t.systemTime() <<
251 "s, cu: " << t.cUserTime() <<
252 "s, cs: " << t.cSystemTime() <<
253 "s, real: " << t.realTime() << "s";
257 ///Class for measuring the cpu time and real time usage of the process
259 ///Class for measuring the cpu time and real time usage of the process.
260 ///It is quite easy-to-use, here is a short example.
262 /// #include<lemon/time_measure.h>
263 /// #include<iostream>
272 /// std::cout << t << '\n';
274 /// doSomethingElse();
275 /// std::cout << t << '\n';
282 ///The \ref Timer can also be \ref stop() "stopped" and
283 ///\ref start() "started" again, so it is possible to compute collected
286 ///\warning Depending on the operation system and its actual configuration
287 ///the time counters have a certain (10ms on a typical Linux system)
289 ///Therefore this tool is not appropriate to measure very short times.
290 ///Also, if you start and stop the timer very frequently, it could lead to
291 ///distorted results.
293 ///\note If you want to measure the running time of the execution of a certain
294 ///function, consider the usage of \ref TimeReport instead.
299 int _running; //Timer is running iff _running>0; (_running>=0 always holds)
300 TimeStamp start_time; //This is the relativ start-time if the timer
301 //is _running, the collected _running time otherwise.
303 void _reset() {if(_running) start_time.stamp(); else start_time.reset();}
308 ///\param run indicates whether or not the timer starts immediately.
310 Timer(bool run=true) :_running(run) {_reset();}
312 ///\name Control the state of the timer
313 ///Basically a Timer can be either running or stopped,
314 ///but it provides a bit finer control on the execution.
315 ///The \ref lemon::Timer "Timer" also counts the number of
316 ///\ref lemon::Timer::start() "start()" executions, and it stops
317 ///only after the same amount (or more) \ref lemon::Timer::stop()
318 ///"stop()"s. This can be useful e.g. to compute the running time
319 ///of recursive functions.
323 ///Reset and stop the time counters
325 ///This function resets and stops the time counters
333 ///Start the time counters
335 ///This function starts the time counters.
337 ///If the timer is started more than ones, it will remain running
338 ///until the same amount of \ref stop() is called.
342 if(_running) _running++;
347 start_time=t-start_time;
352 ///Stop the time counters
354 ///This function stops the time counters. If start() was executed more than
355 ///once, then the same number of stop() execution is necessary the really
365 if(_running && !--_running) {
368 start_time=t-start_time;
372 ///Halt (i.e stop immediately) the time counters
374 ///This function stops immediately the time counters, i.e. <tt>t.halt()</tt>
376 ///equivalent of the following.
378 /// while(t.running()) t.stop()
392 start_time=t-start_time;
396 ///Returns the running state of the timer
398 ///This function returns the number of stop() exections that is
399 ///necessary to really stop the timer.
400 ///For example the timer
401 ///is running if and only if the return value is \c true
402 ///(i.e. greater than
404 int running() { return _running; }
407 ///Restart the time counters
409 ///This function is a shorthand for
410 ///a reset() and a start() calls.
420 ///\name Query Functions for the ellapsed time
424 ///Gives back the ellapsed user time of the process
425 double userTime() const
427 return operator TimeStamp().userTime();
429 ///Gives back the ellapsed system time of the process
430 double systemTime() const
432 return operator TimeStamp().systemTime();
434 ///Gives back the ellapsed user time of the process' children
436 ///\note On <tt>WIN32</tt> platform this value is not calculated.
438 double cUserTime() const
440 return operator TimeStamp().cUserTime();
442 ///Gives back the ellapsed user time of the process' children
444 ///\note On <tt>WIN32</tt> platform this value is not calculated.
446 double cSystemTime() const
448 return operator TimeStamp().cSystemTime();
450 ///Gives back the ellapsed real time
451 double realTime() const
453 return operator TimeStamp().realTime();
455 ///Computes the ellapsed time
457 ///This conversion computes the ellapsed time, therefore you can print
458 ///the ellapsed time like this.
462 /// std::cout << t << '\n';
464 operator TimeStamp () const
468 return _running?t-start_time:start_time;
475 ///Same as Timer but prints a report on destruction.
477 ///Same as \ref Timer but prints a report on destruction.
478 ///This example shows its usage.
480 /// void myAlg(ListGraph &g,int n)
482 /// TimeReport tr("Running time of myAlg: ");
483 /// ... //Here comes the algorithm
489 class TimeReport : public Timer
497 ///\param title This text will be printed before the ellapsed time.
498 ///\param os The stream to print the report to.
499 ///\param run Sets whether the timer should start immediately.
500 TimeReport(std::string title,std::ostream &os=std::cerr,bool run=true)
501 : Timer(run), _title(title), _os(os){}
502 ///Destructor that prints the ellapsed time
505 _os << _title << *this << std::endl;
509 ///'Do nothing' version of TimeReport
517 NoTimeReport(std::string,std::ostream &,bool) {}
519 NoTimeReport(std::string,std::ostream &) {}
521 NoTimeReport(std::string) {}
525 operator TimeStamp () const { return TimeStamp(); }
530 int running() { return 0; }
532 double userTime() const { return 0; }
533 double systemTime() const { return 0; }
534 double cUserTime() const { return 0; }
535 double cSystemTime() const { return 0; }
536 double realTime() const { return 0; }
539 ///Tool to measure the running time more exactly.
541 ///This function calls \c f several times and returns the average
542 ///running time. The number of the executions will be choosen in such a way
543 ///that the full real running time will be roughly between \c min_time
544 ///and <tt>2*min_time</tt>.
545 ///\param f the function object to be measured.
546 ///\param min_time the minimum total running time.
547 ///\retval num if it is not \c NULL, then the actual
548 /// number of execution of \c f will be written into <tt>*num</tt>.
549 ///\retval full_time if it is not \c NULL, then the actual
550 /// total running time will be written into <tt>*full_time</tt>.
551 ///\return The average running time of \c f.
554 TimeStamp runningTimeTest(F f,double min_time=10,unsigned int *num = NULL,
555 TimeStamp *full_time=NULL)
558 unsigned int total=0;
560 for(unsigned int tn=1;tn <= 1U<<31 && full.realTime()<=min_time; tn*=2) {
561 for(;total<tn;total++) f();
565 if(full_time) *full_time=full;
574 #endif //LEMON_TIME_MEASURE_H