COIN-OR::LEMON - Graph Library

source: lemon-0.x/lemon/time_measure.h @ 2028:d0e8a86a1ff2

Last change on this file since 2028:d0e8a86a1ff2 was 2028:d0e8a86a1ff2, checked in by Balazs Dezso, 13 years ago

MinGW32 compatibility

File size: 13.5 KB
RevLine 
[906]1/* -*- C++ -*-
2 *
[1956]3 * This file is a part of LEMON, a generic C++ optimization library
4 *
5 * Copyright (C) 2003-2006
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
[1359]7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
[906]8 *
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.
12 *
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
15 * purpose.
16 *
17 */
18
[921]19#ifndef LEMON_TIME_MEASURE_H
20#define LEMON_TIME_MEASURE_H
[428]21
[1847]22///\ingroup timecount
[428]23///\file
24///\brief Tools for measuring cpu usage
25
[2027]26
27#ifdef WIN32
[2028]28#include <lemon/bits/mingw32_time.h>
[2027]29#else
30#include <sys/times.h>
31#endif
32
[428]33#include <sys/time.h>
34#include <fstream>
35#include <iostream>
36#include <unistd.h>
37
[921]38namespace lemon {
[428]39
[1847]40  /// \addtogroup timecount
[428]41  /// @{
42
43  /// A class to store (cpu)time instances.
44
45  /// This class stores five time values.
46  /// - a real time
47  /// - a user cpu time
48  /// - a system cpu time
49  /// - a user cpu time of children
50  /// - a system cpu time of children
51  ///
52  /// TimeStamp's can be added to or substracted from each other and
53  /// they can be pushed to a stream.
[458]54  ///
[1851]55  /// In most cases, perhaps the \ref Timer or the \ref TimeReport
56  /// class is what you want to use instead.
[458]57  ///
58  ///\author Alpar Juttner
[428]59
60  class TimeStamp
61  {
[1689]62    struct rtms
63    {
64      double tms_utime;
65      double tms_stime;
66      double tms_cutime;
67      double tms_cstime;
68      rtms() {}
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) {}
71    };
72    rtms ts;
[428]73    double real_time;
74 
[1689]75    rtms &getTms() {return ts;}
76    const rtms &getTms() const {return ts;}
77
[2028]78    void _reset() {
79      ts.tms_utime = ts.tms_stime = ts.tms_cutime = ts.tms_cstime = 0;
80      real_time = 0;
81    }
[1780]82
[428]83  public:
84
85    ///Read the current time values of the process
86    void stamp()
87    {
88      timeval tv;
[1689]89      tms _ts;
90      times(&_ts);
[428]91      gettimeofday(&tv, 0);real_time=tv.tv_sec+double(tv.tv_usec)/1e6;
[1689]92      ts=_ts;
[428]93    }
94 
95    /// Constructor initializing with zero
96    TimeStamp()
[1780]97    { _reset(); }
[428]98    ///Constructor initializing with the current time values of the process
99    TimeStamp(void *) { stamp();}
100 
[1780]101    ///Set every time value to zero
102    TimeStamp &reset() {_reset();return *this;}
103
[1005]104    ///\e
[428]105    TimeStamp &operator+=(const TimeStamp &b)
106    {
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;
112      return *this;
113    }
[1005]114    ///\e
[428]115    TimeStamp operator+(const TimeStamp &b) const
116    {
117      TimeStamp t(*this);
118      return t+=b;
119    }
[1005]120    ///\e
[428]121    TimeStamp &operator-=(const TimeStamp &b)
122    {
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;
128      return *this;
129    }
[1005]130    ///\e
[428]131    TimeStamp operator-(const TimeStamp &b) const
132    {
133      TimeStamp t(*this);
134      return t-=b;
135    }
[1689]136    ///\e
137    TimeStamp &operator*=(double b)
138    {
139      ts.tms_utime*=b;
140      ts.tms_stime*=b;
141      ts.tms_cutime*=b;
142      ts.tms_cstime*=b;
143      real_time*=b;
144      return *this;
145    }
146    ///\e
147    TimeStamp operator*(double b) const
148    {
149      TimeStamp t(*this);
150      return t*=b;
151    }
152    friend TimeStamp operator*(double b,const TimeStamp &t);
153    ///\e
154    TimeStamp &operator/=(double b)
155    {
156      ts.tms_utime/=b;
157      ts.tms_stime/=b;
158      ts.tms_cutime/=b;
159      ts.tms_cstime/=b;
160      real_time/=b;
161      return *this;
162    }
163    ///\e
164    TimeStamp operator/(double b) const
165    {
166      TimeStamp t(*this);
167      return t/=b;
168    }
[428]169    ///The time ellapsed since the last call of stamp()
170    TimeStamp ellapsed() const
171    {
172      TimeStamp t(NULL);
173      return t-*this;
174    }
175 
176    friend std::ostream& operator<<(std::ostream& os,const TimeStamp &t);
177 
178    ///Gives back the user time of the process
[1689]179    double userTime() const
[428]180    {
181      return double(ts.tms_utime)/sysconf(_SC_CLK_TCK);
182    }
183    ///Gives back the system time of the process
[1689]184    double systemTime() const
[428]185    {
186      return double(ts.tms_stime)/sysconf(_SC_CLK_TCK);
187    }
188    ///Gives back the user time of the process' children
[1689]189    double cUserTime() const
[428]190    {
191      return double(ts.tms_cutime)/sysconf(_SC_CLK_TCK);
192    }
193    ///Gives back the user time of the process' children
[1689]194    double cSystemTime() const
[428]195    {
196      return double(ts.tms_cstime)/sysconf(_SC_CLK_TCK);
197    }
[1780]198    ///Gives back the real time
[1689]199    double realTime() const {return real_time;}
[428]200  };
201
[1689]202  TimeStamp operator*(double b,const TimeStamp &t)
203  {
204    return t*b;
205  }
206 
[1851]207  ///Prints the time counters
208
209  ///Prints the time counters in the following form:
210  ///
211  /// <tt>u: XX.XXs s: XX.XXs cu: XX.XXs cs: XX.XXs real: XX.XXs</tt>
212  ///
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)
221  {
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";
228    return os;
229  }
230
[1780]231  ///Class for measuring the cpu time and real time usage of the process
[458]232
[1780]233  ///Class for measuring the cpu time and real time usage of the process.
[458]234  ///It is quite easy-to-use, here is a short example.
235  ///\code
[921]236  ///#include<lemon/time_measure.h>
[696]237  ///#include<iostream>
[814]238  ///
[458]239  ///int main()
240  ///{
241  ///
242  ///  ...
243  ///
[696]244  ///  Timer T;
[458]245  ///  doSomething();
[696]246  ///  std::cout << T << '\n';
[1847]247  ///  T.restart();
[458]248  ///  doSomethingElse();
[696]249  ///  std::cout << T << '\n';
[458]250  ///
251  ///  ...
252  ///
253  ///}
254  ///\endcode
255  ///
[1780]256  ///The \ref Timer can also be \ref stop() "stopped" and
[1806]257  ///\ref start() "started" again, so it is possible to compute collected
[1780]258  ///running times.
259  ///
260  ///\warning Depending on the operation system and its actual configuration
[1847]261  ///the time counters have a certain (10ms on a typical Linux system)
262  ///granularity.
[1780]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
265  ///distorted results.
266  ///
[1851]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.
[1780]269  ///
[458]270  ///\todo This shouldn't be Unix (Linux) specific.
[1851]271  ///\sa TimeReport
[458]272  ///
273  ///\author Alpar Juttner
[428]274  class Timer
275  {
[1847]276    int _running; //Timer is running iff _running>0; (_running>=0 always holds)
[1780]277    TimeStamp start_time; //This is the relativ start-time if the timer
[1847]278                          //is _running, the collected _running time otherwise.
[1780]279   
[1847]280    void _reset() {if(_running) start_time.stamp(); else start_time.reset();}
[428]281 
282  public:
[1780]283    ///Constructor.
284
[1953]285    ///\param run indicates whether or not the timer starts immediately.
[1780]286    ///
[1847]287    Timer(bool run=true) :_running(run) {_reset();}
[428]288
[1851]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.
296    ///
[428]297
[1851]298    ///@{
[428]299
[1847]300    ///Reset and stop the time counters
[1069]301
[1847]302    ///This function resets and stops the time counters
303    ///\sa restart()
[1069]304    void reset()
[428]305    {
[1847]306      _running=0;
[428]307      _reset();
308    }
[1005]309
[1780]310    ///Start the time counters
311   
312    ///This function starts the time counters.
313    ///
314    ///If the timer is started more than ones, it will remain running
315    ///until the same amount of \ref stop() is called.
316    ///\sa stop()
317    void start()
318    {
[1847]319      if(_running) _running++;
[1780]320      else {
[1850]321        _running=1;
[1780]322        TimeStamp t;
323        t.stamp();
324        start_time=t-start_time;
325      }
326    }
[1847]327
[1780]328   
329    ///Stop the time counters
[1005]330
[1847]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
333    ///stop the timer.
334    ///
335    ///\sa halt()
336    ///\sa start()
337    ///\sa restart()
338    ///\sa reset()
339
[1780]340    void stop()
341    {
[1847]342      if(_running && !--_running) {
[1780]343        TimeStamp t;
344        t.stamp();
345        start_time=t-start_time;
346      }
347    }
[1847]348
349    ///Halt (i.e stop immediately) the time counters
350
351    ///This function stops immediately the time counters.
352    ///
353    ///\sa stop()
354    ///\sa restart()
355    ///\sa reset()
356
357    void halt()
358    {
359      if(_running) {
360        _running=0;
361        TimeStamp t;
362        t.stamp();
363        start_time=t-start_time;
364      }
365    }
366
367    ///Returns the running state of the timer
368
369    ///This function returns the number of stop() exections that is
370    ///necessary to really stop the timer.
371    ///For example the timer
372    ///is running if and only if the return value is \c true
373    ///(i.e. greater than
374    ///zero).
375    int running()  { return _running; }
376   
377   
378    ///Restart the time counters
379
380    ///This function is a shorthand for
381    ///a reset() and a start() calls.
382    ///
383    void restart()
384    {
385      reset();
386      start();
387    }
[1780]388   
[1851]389    ///@}
390
391    ///\name Query Functions for the ellapsed time
392
393    ///@{
394
[1005]395    ///Gives back the ellapsed user time of the process
[1689]396    double userTime() const
[1005]397    {
[1689]398      return operator TimeStamp().userTime();
[1005]399    }
400    ///Gives back the ellapsed system time of the process
[1689]401    double systemTime() const
[1005]402    {
[1689]403      return operator TimeStamp().systemTime();
[1005]404    }
405    ///Gives back the ellapsed user time of the process' children
[1689]406    double cUserTime() const
[1005]407    {
[1689]408      return operator TimeStamp().cUserTime();
[1005]409    }
410    ///Gives back the ellapsed user time of the process' children
[1689]411    double cSystemTime() const
[1005]412    {
[1689]413      return operator TimeStamp().cSystemTime();
[1005]414    }
[1780]415    ///Gives back the ellapsed real time
[1689]416    double realTime() const
[1005]417    {
[1689]418      return operator TimeStamp().realTime();
[1005]419    }
[1851]420    ///Computes the ellapsed time
[1005]421
[1851]422    ///This conversion computes the ellapsed time, therefore you can print
423    ///the ellapsed time like this.
424    ///\code
425    ///  Timer T;
426    ///  doSomething();
427    ///  std::cout << T << '\n';
428    ///\endcode
429    operator TimeStamp () const
430    {
431      TimeStamp t;
432      t.stamp();
433      return _running?t-start_time:start_time;
434    }
435
436
437    ///@}
[428]438  };
439
[1847]440  ///Same as \ref Timer but prints a report on destruction.
441
442  ///Same as \ref Timer but prints a report on destruction.
[1851]443  ///This example shows its usage.
444  ///\code
445  ///  void myAlg(ListGraph &g,int n)
446  ///  {
447  ///    TimeReport TR("Running time of myAlg: ");
448  ///    ... //Here comes the algorithm
449  ///  }
450  ///\endcode
451  ///
452  ///\sa Timer
453  ///\sa NoTimeReport
454  ///\todo There is no test case for this
[1847]455  class TimeReport : public Timer
456  {
457    std::string _title;
458    std::ostream &_os;
459  public:
460    ///\e
[1851]461
462    ///\param title This text will be printed before the ellapsed time.
463    ///\param os The stream to print the report to.
464    ///\param run Sets whether the timer should start immediately.
465
466    TimeReport(std::string title,std::ostream &os=std::cerr,bool run=true)
[1847]467      : Timer(run), _title(title), _os(os){}
[1851]468    ///\e Prints the ellapsed time on destruction.
[1847]469    ~TimeReport()
470    {
[1851]471      _os << _title << *this << std::endl;
[1847]472    }
473  };
474     
[1851]475  ///'Do nothing' version of \ref TimeReport
[428]476
[1851]477  ///\sa TimeReport
[428]478  ///
[1851]479  class NoTimeReport
[428]480  {
[1851]481  public:
482    ///\e
[1855]483    NoTimeReport(std::string,std::ostream &,bool) {}
484    ///\e
485    NoTimeReport(std::string,std::ostream &) {}
486    ///\e
487    NoTimeReport(std::string) {}
[1851]488    ///\e Do nothing.
489    ~NoTimeReport() {}
[428]490
[1851]491    operator TimeStamp () const { return TimeStamp(); }
492    void reset() {}
493    void start() {}
494    void stop() {}
495    void halt() {}
496    int running() { return 0; }
497    void restart() {}
498    double userTime() const { return 0; }
499    double systemTime() const { return 0; }
500    double cUserTime() const { return 0; }
501    double cSystemTime() const { return 0; }
502    double realTime() const { return 0; }
503  };
504     
[1689]505  ///Tool to measure the running time more exactly.
506 
507  ///This function calls \c f several times and returns the average
508  ///running time. The number of the executions will be choosen in such a way
[1780]509  ///that the full real running time will be roughly between \c min_time
[1689]510  ///and <tt>2*min_time</tt>.
511  ///\param f the function object to be measured.
512  ///\param min_time the minimum total running time.
[1894]513  ///\retval num if it is not \c NULL, then the actual
514  ///        number of execution of \c f will be written into <tt>*num</tt>.
515  ///\retval full_time if it is not \c NULL, then the actual
516  ///        total running time will be written into <tt>*full_time</tt>.
[1689]517  ///\return The average running time of \c f.
518 
519  template<class F>
[1839]520  TimeStamp runningTimeTest(F f,double min_time=10,int *num = NULL,
[2027]521                            TimeStamp *full_time=NULL)
[1689]522  {
523    Timer t;
524    TimeStamp full;
525    int total=0;
[1960]526    for(int tn=1;tn < 1<<30; tn*=2) {
[1811]527      for(;total<tn;total++) f();
[1689]528      full=t;
529      if(full.realTime()>min_time) {
530        if(num) *num=total;
531        if(full_time) *full_time=full;
532      return full/total;
533      }
534    }
535    return TimeStamp();
536  }
537 
[428]538  /// @} 
539
[1689]540
[921]541} //namespace lemon
[428]542
[921]543#endif //LEMON_TIME_MEASURE_H
Note: See TracBrowser for help on using the repository browser.