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
33 int gettimeofday(struct timeval * tp, struct timezone *) {
39 time_t theTime = time(NULL);
41 tmrec = *localtime(&theTime);
42 tp->tv_sec = mktime(&tmrec);
43 GetLocalTime(&systime); /* system time */
45 tp->tv_usec = systime.wMilliseconds * 1000;
60 static long filetime_to_clock(FILETIME *ft)
62 __int64 qw = ft->dwHighDateTime;
64 qw |= ft->dwLowDateTime;
65 qw /= 10000; /* File time ticks at 0.1uS, clock at 1mS */
70 int times(struct tms *tmbuf)
72 FILETIME create, exit, kernel, user;
73 if (GetProcessTimes(GetCurrentProcess(),&create, &exit, &kernel, &user)) {
74 tmbuf->tms_utime = filetime_to_clock(&user);
75 tmbuf->tms_stime = filetime_to_clock(&kernel);
76 tmbuf->tms_cutime = 0;
77 tmbuf->tms_cstime = 0;
80 tmbuf->tms_utime = clock();
82 tmbuf->tms_cutime = 0;
83 tmbuf->tms_cstime = 0;
96 #include <sys/times.h>
106 /// \addtogroup timecount
109 /// A class to store (cpu)time instances.
111 /// This class stores five time values.
113 /// - a user cpu time
114 /// - a system cpu time
115 /// - a user cpu time of children
116 /// - a system cpu time of children
118 /// TimeStamp's can be added to or substracted from each other and
119 /// they can be pushed to a stream.
121 /// In most cases, perhaps the \ref Timer or the \ref TimeReport
122 /// class is what you want to use instead.
124 ///\author Alpar Juttner
135 rtms(tms ts) : tms_utime(ts.tms_utime), tms_stime(ts.tms_stime),
136 tms_cutime(ts.tms_cutime), tms_cstime(ts.tms_cstime) {}
141 rtms &getTms() {return ts;}
142 const rtms &getTms() const {return ts;}
145 { ts.tms_utime=ts.tms_stime=ts.tms_cutime=ts.tms_cstime=0; real_time=0;}
149 ///Read the current time values of the process
155 gettimeofday(&tv, 0);real_time=tv.tv_sec+double(tv.tv_usec)/1e6;
159 /// Constructor initializing with zero
162 ///Constructor initializing with the current time values of the process
163 TimeStamp(void *) { stamp();}
165 ///Set every time value to zero
166 TimeStamp &reset() {_reset();return *this;}
169 TimeStamp &operator+=(const TimeStamp &b)
171 ts.tms_utime+=b.ts.tms_utime;
172 ts.tms_stime+=b.ts.tms_stime;
173 ts.tms_cutime+=b.ts.tms_cutime;
174 ts.tms_cstime+=b.ts.tms_cstime;
175 real_time+=b.real_time;
179 TimeStamp operator+(const TimeStamp &b) const
185 TimeStamp &operator-=(const TimeStamp &b)
187 ts.tms_utime-=b.ts.tms_utime;
188 ts.tms_stime-=b.ts.tms_stime;
189 ts.tms_cutime-=b.ts.tms_cutime;
190 ts.tms_cstime-=b.ts.tms_cstime;
191 real_time-=b.real_time;
195 TimeStamp operator-(const TimeStamp &b) const
201 TimeStamp &operator*=(double b)
211 TimeStamp operator*(double b) const
216 friend TimeStamp operator*(double b,const TimeStamp &t);
218 TimeStamp &operator/=(double b)
228 TimeStamp operator/(double b) const
233 ///The time ellapsed since the last call of stamp()
234 TimeStamp ellapsed() const
240 friend std::ostream& operator<<(std::ostream& os,const TimeStamp &t);
242 ///Gives back the user time of the process
243 double userTime() const
245 return double(ts.tms_utime)/sysconf(_SC_CLK_TCK);
247 ///Gives back the system time of the process
248 double systemTime() const
250 return double(ts.tms_stime)/sysconf(_SC_CLK_TCK);
252 ///Gives back the user time of the process' children
253 double cUserTime() const
255 return double(ts.tms_cutime)/sysconf(_SC_CLK_TCK);
257 ///Gives back the user time of the process' children
258 double cSystemTime() const
260 return double(ts.tms_cstime)/sysconf(_SC_CLK_TCK);
262 ///Gives back the real time
263 double realTime() const {return real_time;}
266 TimeStamp operator*(double b,const TimeStamp &t)
271 ///Prints the time counters
273 ///Prints the time counters in the following form:
275 /// <tt>u: XX.XXs s: XX.XXs cu: XX.XXs cs: XX.XXs real: XX.XXs</tt>
277 /// where the values are the
278 /// \li \c u: user cpu time,
279 /// \li \c s: system cpu time,
280 /// \li \c cu: user cpu time of children,
281 /// \li \c cs: system cpu time of children,
282 /// \li \c real: real time.
283 /// \relates TimeStamp
284 inline std::ostream& operator<<(std::ostream& os,const TimeStamp &t)
286 long cls = sysconf(_SC_CLK_TCK);
287 os << "u: " << double(t.getTms().tms_utime)/cls <<
288 "s, s: " << double(t.getTms().tms_stime)/cls <<
289 "s, cu: " << double(t.getTms().tms_cutime)/cls <<
290 "s, cs: " << double(t.getTms().tms_cstime)/cls <<
291 "s, real: " << t.realTime() << "s";
295 ///Class for measuring the cpu time and real time usage of the process
297 ///Class for measuring the cpu time and real time usage of the process.
298 ///It is quite easy-to-use, here is a short example.
300 ///#include<lemon/time_measure.h>
301 ///#include<iostream>
310 /// std::cout << T << '\n';
312 /// doSomethingElse();
313 /// std::cout << T << '\n';
320 ///The \ref Timer can also be \ref stop() "stopped" and
321 ///\ref start() "started" again, so it is possible to compute collected
324 ///\warning Depending on the operation system and its actual configuration
325 ///the time counters have a certain (10ms on a typical Linux system)
327 ///Therefore this tool is not appropriate to measure very short times.
328 ///Also, if you start and stop the timer very frequently, it could lead
329 ///distorted results.
331 ///\note If you want to measure the running time of the execution of a certain
332 ///function, consider the usage of \ref TimeReport instead.
334 ///\todo This shouldn't be Unix (Linux) specific.
337 ///\author Alpar Juttner
340 int _running; //Timer is running iff _running>0; (_running>=0 always holds)
341 TimeStamp start_time; //This is the relativ start-time if the timer
342 //is _running, the collected _running time otherwise.
344 void _reset() {if(_running) start_time.stamp(); else start_time.reset();}
349 ///\param run indicates whether or not the timer starts immediately.
351 Timer(bool run=true) :_running(run) {_reset();}
353 ///\name Control the state of the timer
354 ///Basically a Timer can be either running or stopped,
355 ///but it provides a bit finer control on the execution.
356 ///The \ref Timer also counts the number of \ref start()
357 ///executions, and is stops only after the same amount (or more)
358 ///\ref stop() "stop()"s. This can be useful e.g. to compute the running time
359 ///of recursive functions.
364 ///Reset and stop the time counters
366 ///This function resets and stops the time counters
374 ///Start the time counters
376 ///This function starts the time counters.
378 ///If the timer is started more than ones, it will remain running
379 ///until the same amount of \ref stop() is called.
383 if(_running) _running++;
388 start_time=t-start_time;
393 ///Stop the time counters
395 ///This function stops the time counters. If start() was executed more than
396 ///once, then the same number of stop() execution is necessary the really
406 if(_running && !--_running) {
409 start_time=t-start_time;
413 ///Halt (i.e stop immediately) the time counters
415 ///This function stops immediately the time counters.
427 start_time=t-start_time;
431 ///Returns the running state of the timer
433 ///This function returns the number of stop() exections that is
434 ///necessary to really stop the timer.
435 ///For example the timer
436 ///is running if and only if the return value is \c true
437 ///(i.e. greater than
439 int running() { return _running; }
442 ///Restart the time counters
444 ///This function is a shorthand for
445 ///a reset() and a start() calls.
455 ///\name Query Functions for the ellapsed time
459 ///Gives back the ellapsed user time of the process
460 double userTime() const
462 return operator TimeStamp().userTime();
464 ///Gives back the ellapsed system time of the process
465 double systemTime() const
467 return operator TimeStamp().systemTime();
469 ///Gives back the ellapsed user time of the process' children
470 double cUserTime() const
472 return operator TimeStamp().cUserTime();
474 ///Gives back the ellapsed user time of the process' children
475 double cSystemTime() const
477 return operator TimeStamp().cSystemTime();
479 ///Gives back the ellapsed real time
480 double realTime() const
482 return operator TimeStamp().realTime();
484 ///Computes the ellapsed time
486 ///This conversion computes the ellapsed time, therefore you can print
487 ///the ellapsed time like this.
491 /// std::cout << T << '\n';
493 operator TimeStamp () const
497 return _running?t-start_time:start_time;
504 ///Same as \ref Timer but prints a report on destruction.
506 ///Same as \ref Timer but prints a report on destruction.
507 ///This example shows its usage.
509 /// void myAlg(ListGraph &g,int n)
511 /// TimeReport TR("Running time of myAlg: ");
512 /// ... //Here comes the algorithm
518 ///\todo There is no test case for this
519 class TimeReport : public Timer
526 ///\param title This text will be printed before the ellapsed time.
527 ///\param os The stream to print the report to.
528 ///\param run Sets whether the timer should start immediately.
530 TimeReport(std::string title,std::ostream &os=std::cerr,bool run=true)
531 : Timer(run), _title(title), _os(os){}
532 ///\e Prints the ellapsed time on destruction.
535 _os << _title << *this << std::endl;
539 ///'Do nothing' version of \ref TimeReport
547 NoTimeReport(std::string,std::ostream &,bool) {}
549 NoTimeReport(std::string,std::ostream &) {}
551 NoTimeReport(std::string) {}
555 operator TimeStamp () const { return TimeStamp(); }
560 int running() { return 0; }
562 double userTime() const { return 0; }
563 double systemTime() const { return 0; }
564 double cUserTime() const { return 0; }
565 double cSystemTime() const { return 0; }
566 double realTime() const { return 0; }
569 ///Tool to measure the running time more exactly.
571 ///This function calls \c f several times and returns the average
572 ///running time. The number of the executions will be choosen in such a way
573 ///that the full real running time will be roughly between \c min_time
574 ///and <tt>2*min_time</tt>.
575 ///\param f the function object to be measured.
576 ///\param min_time the minimum total running time.
577 ///\retval num if it is not \c NULL, then the actual
578 /// number of execution of \c f will be written into <tt>*num</tt>.
579 ///\retval full_time if it is not \c NULL, then the actual
580 /// total running time will be written into <tt>*full_time</tt>.
581 ///\return The average running time of \c f.
584 TimeStamp runningTimeTest(F f,double min_time=10,int *num = NULL,
585 TimeStamp *full_time=NULL)
590 for(int tn=1;tn < 1<<30; tn*=2) {
591 for(;total<tn;total++) f();
593 if(full.realTime()>min_time) {
595 if(full_time) *full_time=full;
607 #endif //LEMON_TIME_MEASURE_H