Update INSTALL to use CMAKE. Also update AUTHORS and LICENSE
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 #include <lemon/bits/windows.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.
67 utime = stime = cutime = cstime = rtime = 0;
72 ///Read the current time values of the process
78 rtime=tv.tv_sec+double(tv.tv_usec)/1e6;
81 double tck=sysconf(_SC_CLK_TCK);
83 utime=ts.tms_utime/tck;
84 stime=ts.tms_stime/tck;
85 cutime=ts.tms_cutime/tck;
86 cstime=ts.tms_cstime/tck;
88 bits::getWinProcTimes(rtime, utime, stime, cutime, cstime);
92 /// Constructor initializing with zero
95 ///Constructor initializing with the current time values of the process
96 TimeStamp(void *) { stamp();}
98 ///Set every time value to zero
99 TimeStamp &reset() {_reset();return *this;}
102 TimeStamp &operator+=(const TimeStamp &b)
112 TimeStamp operator+(const TimeStamp &b) const
118 TimeStamp &operator-=(const TimeStamp &b)
128 TimeStamp operator-(const TimeStamp &b) const
134 TimeStamp &operator*=(double b)
144 TimeStamp operator*(double b) const
149 friend TimeStamp operator*(double b,const TimeStamp &t);
151 TimeStamp &operator/=(double b)
161 TimeStamp operator/(double b) const
166 ///The time ellapsed since the last call of stamp()
167 TimeStamp ellapsed() const
173 friend std::ostream& operator<<(std::ostream& os,const TimeStamp &t);
175 ///Gives back the user time of the process
176 double userTime() const
180 ///Gives back the system time of the process
181 double systemTime() const
185 ///Gives back the user time of the process' children
187 ///\note On <tt>WIN32</tt> platform this value is not calculated.
189 double cUserTime() const
193 ///Gives back the user time of the process' children
195 ///\note On <tt>WIN32</tt> platform this value is not calculated.
197 double cSystemTime() const
201 ///Gives back the real time
202 double realTime() const {return rtime;}
205 inline TimeStamp operator*(double b,const TimeStamp &t)
210 ///Prints the time counters
212 ///Prints the time counters in the following form:
214 /// <tt>u: XX.XXs s: XX.XXs cu: XX.XXs cs: XX.XXs real: XX.XXs</tt>
216 /// where the values are the
217 /// \li \c u: user cpu time,
218 /// \li \c s: system cpu time,
219 /// \li \c cu: user cpu time of children,
220 /// \li \c cs: system cpu time of children,
221 /// \li \c real: real time.
222 /// \relates TimeStamp
223 /// \note On <tt>WIN32</tt> platform the cummulative values are not
225 inline std::ostream& operator<<(std::ostream& os,const TimeStamp &t)
227 os << "u: " << t.userTime() <<
228 "s, s: " << t.systemTime() <<
229 "s, cu: " << t.cUserTime() <<
230 "s, cs: " << t.cSystemTime() <<
231 "s, real: " << t.realTime() << "s";
235 ///Class for measuring the cpu time and real time usage of the process
237 ///Class for measuring the cpu time and real time usage of the process.
238 ///It is quite easy-to-use, here is a short example.
240 /// #include<lemon/time_measure.h>
241 /// #include<iostream>
250 /// std::cout << t << '\n';
252 /// doSomethingElse();
253 /// std::cout << t << '\n';
260 ///The \ref Timer can also be \ref stop() "stopped" and
261 ///\ref start() "started" again, so it is possible to compute collected
264 ///\warning Depending on the operation system and its actual configuration
265 ///the time counters have a certain (10ms on a typical Linux system)
267 ///Therefore this tool is not appropriate to measure very short times.
268 ///Also, if you start and stop the timer very frequently, it could lead to
269 ///distorted results.
271 ///\note If you want to measure the running time of the execution of a certain
272 ///function, consider the usage of \ref TimeReport instead.
277 int _running; //Timer is running iff _running>0; (_running>=0 always holds)
278 TimeStamp start_time; //This is the relativ start-time if the timer
279 //is _running, the collected _running time otherwise.
281 void _reset() {if(_running) start_time.stamp(); else start_time.reset();}
286 ///\param run indicates whether or not the timer starts immediately.
288 Timer(bool run=true) :_running(run) {_reset();}
290 ///\name Control the State of the Timer
291 ///Basically a Timer can be either running or stopped,
292 ///but it provides a bit finer control on the execution.
293 ///The \ref lemon::Timer "Timer" also counts the number of
294 ///\ref lemon::Timer::start() "start()" executions, and it stops
295 ///only after the same amount (or more) \ref lemon::Timer::stop()
296 ///"stop()"s. This can be useful e.g. to compute the running time
297 ///of recursive functions.
301 ///Reset and stop the time counters
303 ///This function resets and stops the time counters
311 ///Start the time counters
313 ///This function starts the time counters.
315 ///If the timer is started more than ones, it will remain running
316 ///until the same amount of \ref stop() is called.
320 if(_running) _running++;
325 start_time=t-start_time;
330 ///Stop the time counters
332 ///This function stops the time counters. If start() was executed more than
333 ///once, then the same number of stop() execution is necessary the really
343 if(_running && !--_running) {
346 start_time=t-start_time;
350 ///Halt (i.e stop immediately) the time counters
352 ///This function stops immediately the time counters, i.e. <tt>t.halt()</tt>
354 ///equivalent of the following.
356 /// while(t.running()) t.stop()
370 start_time=t-start_time;
374 ///Returns the running state of the timer
376 ///This function returns the number of stop() exections that is
377 ///necessary to really stop the timer.
378 ///For example, the timer
379 ///is running if and only if the return value is \c true
380 ///(i.e. greater than
382 int running() { return _running; }
385 ///Restart the time counters
387 ///This function is a shorthand for
388 ///a reset() and a start() calls.
398 ///\name Query Functions for the Ellapsed Time
402 ///Gives back the ellapsed user time of the process
403 double userTime() const
405 return operator TimeStamp().userTime();
407 ///Gives back the ellapsed system time of the process
408 double systemTime() const
410 return operator TimeStamp().systemTime();
412 ///Gives back the ellapsed user time of the process' children
414 ///\note On <tt>WIN32</tt> platform this value is not calculated.
416 double cUserTime() const
418 return operator TimeStamp().cUserTime();
420 ///Gives back the ellapsed user time of the process' children
422 ///\note On <tt>WIN32</tt> platform this value is not calculated.
424 double cSystemTime() const
426 return operator TimeStamp().cSystemTime();
428 ///Gives back the ellapsed real time
429 double realTime() const
431 return operator TimeStamp().realTime();
433 ///Computes the ellapsed time
435 ///This conversion computes the ellapsed time, therefore you can print
436 ///the ellapsed time like this.
440 /// std::cout << t << '\n';
442 operator TimeStamp () const
446 return _running?t-start_time:start_time;
453 ///Same as Timer but prints a report on destruction.
455 ///Same as \ref Timer but prints a report on destruction.
456 ///This example shows its usage.
458 /// void myAlg(ListGraph &g,int n)
460 /// TimeReport tr("Running time of myAlg: ");
461 /// ... //Here comes the algorithm
467 class TimeReport : public Timer
475 ///\param title This text will be printed before the ellapsed time.
476 ///\param os The stream to print the report to.
477 ///\param run Sets whether the timer should start immediately.
478 TimeReport(std::string title,std::ostream &os=std::cerr,bool run=true)
479 : Timer(run), _title(title), _os(os){}
480 ///Destructor that prints the ellapsed time
483 _os << _title << *this << std::endl;
487 ///'Do nothing' version of TimeReport
495 NoTimeReport(std::string,std::ostream &,bool) {}
497 NoTimeReport(std::string,std::ostream &) {}
499 NoTimeReport(std::string) {}
503 operator TimeStamp () const { return TimeStamp(); }
508 int running() { return 0; }
510 double userTime() const { return 0; }
511 double systemTime() const { return 0; }
512 double cUserTime() const { return 0; }
513 double cSystemTime() const { return 0; }
514 double realTime() const { return 0; }
517 ///Tool to measure the running time more exactly.
519 ///This function calls \c f several times and returns the average
520 ///running time. The number of the executions will be choosen in such a way
521 ///that the full real running time will be roughly between \c min_time
522 ///and <tt>2*min_time</tt>.
523 ///\param f the function object to be measured.
524 ///\param min_time the minimum total running time.
525 ///\retval num if it is not \c NULL, then the actual
526 /// number of execution of \c f will be written into <tt>*num</tt>.
527 ///\retval full_time if it is not \c NULL, then the actual
528 /// total running time will be written into <tt>*full_time</tt>.
529 ///\return The average running time of \c f.
532 TimeStamp runningTimeTest(F f,double min_time=10,unsigned int *num = NULL,
533 TimeStamp *full_time=NULL)
536 unsigned int total=0;
538 for(unsigned int tn=1;tn <= 1U<<31 && full.realTime()<=min_time; tn*=2) {
539 for(;total<tn;total++) f();
543 if(full_time) *full_time=full;
552 #endif //LEMON_TIME_MEASURE_H