1.1 --- a/lemon/time_measure.h Tue Dec 06 11:59:44 2005 +0000
1.2 +++ b/lemon/time_measure.h Tue Dec 06 18:44:26 2005 +0000
1.3 @@ -44,7 +44,8 @@
1.4 /// TimeStamp's can be added to or substracted from each other and
1.5 /// they can be pushed to a stream.
1.6 ///
1.7 - /// In most cases, perhaps \ref Timer class is what you want to use instead.
1.8 + /// In most cases, perhaps the \ref Timer or the \ref TimeReport
1.9 + /// class is what you want to use instead.
1.10 ///
1.11 ///\author Alpar Juttner
1.12
1.13 @@ -193,6 +194,30 @@
1.14 return t*b;
1.15 }
1.16
1.17 + ///Prints the time counters
1.18 +
1.19 + ///Prints the time counters in the following form:
1.20 + ///
1.21 + /// <tt>u: XX.XXs s: XX.XXs cu: XX.XXs cs: XX.XXs real: XX.XXs</tt>
1.22 + ///
1.23 + /// where the values are the
1.24 + /// \li \c u: user cpu time,
1.25 + /// \li \c s: system cpu time,
1.26 + /// \li \c cu: user cpu time of children,
1.27 + /// \li \c cs: system cpu time of children,
1.28 + /// \li \c real: real time.
1.29 + /// \relates TimeStamp
1.30 + inline std::ostream& operator<<(std::ostream& os,const TimeStamp &t)
1.31 + {
1.32 + long cls = sysconf(_SC_CLK_TCK);
1.33 + os << "u: " << double(t.getTms().tms_utime)/cls <<
1.34 + "s, s: " << double(t.getTms().tms_stime)/cls <<
1.35 + "s, cu: " << double(t.getTms().tms_cutime)/cls <<
1.36 + "s, cs: " << double(t.getTms().tms_cstime)/cls <<
1.37 + "s, real: " << t.realTime() << "s";
1.38 + return os;
1.39 + }
1.40 +
1.41 ///Class for measuring the cpu time and real time usage of the process
1.42
1.43 ///Class for measuring the cpu time and real time usage of the process.
1.44 @@ -229,12 +254,11 @@
1.45 ///Also, if you start and stop the timer very frequently, it could lead
1.46 ///distorted results.
1.47 ///
1.48 - ///The \ref Timer also counts the number of \ref start()
1.49 - ///executions, and is stops only after the same amount (or more)
1.50 - ///\ref stop() "stop()"s. This can be useful e.g. to compute the running time
1.51 - ///of recursive functions.
1.52 + ///\note If you want to measure the running time of the execution of a certain
1.53 + ///function, consider the usage of \ref TimeReport instead.
1.54 ///
1.55 ///\todo This shouldn't be Unix (Linux) specific.
1.56 + ///\sa TimeReport
1.57 ///
1.58 ///\author Alpar Juttner
1.59 class Timer
1.60 @@ -252,16 +276,16 @@
1.61 ///
1.62 Timer(bool run=true) :_running(run) {_reset();}
1.63
1.64 - ///Computes the ellapsed time
1.65 + ///\name Control the state of the timer
1.66 + ///Basically a Timer can be either running or stopped,
1.67 + ///but it provides a bit finer control on the execution.
1.68 + ///The \ref Timer also counts the number of \ref start()
1.69 + ///executions, and is stops only after the same amount (or more)
1.70 + ///\ref stop() "stop()"s. This can be useful e.g. to compute the running time
1.71 + ///of recursive functions.
1.72 + ///
1.73
1.74 - ///This conversion computes the ellapsed time
1.75 - ///
1.76 - operator TimeStamp () const
1.77 - {
1.78 - TimeStamp t;
1.79 - t.stamp();
1.80 - return _running?t-start_time:start_time;
1.81 - }
1.82 + ///@{
1.83
1.84 ///Reset and stop the time counters
1.85
1.86 @@ -352,6 +376,12 @@
1.87 start();
1.88 }
1.89
1.90 + ///@}
1.91 +
1.92 + ///\name Query Functions for the ellapsed time
1.93 +
1.94 + ///@{
1.95 +
1.96 ///Gives back the ellapsed user time of the process
1.97 double userTime() const
1.98 {
1.99 @@ -377,53 +407,87 @@
1.100 {
1.101 return operator TimeStamp().realTime();
1.102 }
1.103 + ///Computes the ellapsed time
1.104
1.105 + ///This conversion computes the ellapsed time, therefore you can print
1.106 + ///the ellapsed time like this.
1.107 + ///\code
1.108 + /// Timer T;
1.109 + /// doSomething();
1.110 + /// std::cout << T << '\n';
1.111 + ///\endcode
1.112 + operator TimeStamp () const
1.113 + {
1.114 + TimeStamp t;
1.115 + t.stamp();
1.116 + return _running?t-start_time:start_time;
1.117 + }
1.118 +
1.119 +
1.120 + ///@}
1.121 };
1.122
1.123 ///Same as \ref Timer but prints a report on destruction.
1.124
1.125 ///Same as \ref Timer but prints a report on destruction.
1.126 - ///\todo Untested
1.127 + ///This example shows its usage.
1.128 + ///\code
1.129 + /// void myAlg(ListGraph &g,int n)
1.130 + /// {
1.131 + /// TimeReport TR("Running time of myAlg: ");
1.132 + /// ... //Here comes the algorithm
1.133 + /// }
1.134 + ///\endcode
1.135 + ///
1.136 + ///\sa Timer
1.137 + ///\sa NoTimeReport
1.138 + ///\todo There is no test case for this
1.139 class TimeReport : public Timer
1.140 {
1.141 std::string _title;
1.142 std::ostream &_os;
1.143 public:
1.144 ///\e
1.145 -
1.146 - TimeReport(std::string title,std::ostream &os,bool run)
1.147 +
1.148 + ///\param title This text will be printed before the ellapsed time.
1.149 + ///\param os The stream to print the report to.
1.150 + ///\param run Sets whether the timer should start immediately.
1.151 +
1.152 + TimeReport(std::string title,std::ostream &os=std::cerr,bool run=true)
1.153 : Timer(run), _title(title), _os(os){}
1.154 + ///\e Prints the ellapsed time on destruction.
1.155 ~TimeReport()
1.156 {
1.157 - _os << _title << this << std::endl;
1.158 + _os << _title << *this << std::endl;
1.159 }
1.160 };
1.161
1.162 - ///Prints the time counters
1.163 + ///'Do nothing' version of \ref TimeReport
1.164
1.165 - ///Prints the time counters in the following form:
1.166 + ///\sa TimeReport
1.167 ///
1.168 - /// <tt>u: XX.XXs s: XX.XXs cu: XX.XXs cs: XX.XXs real: XX.XXs</tt>
1.169 - ///
1.170 - /// where the values are the
1.171 - /// \li \c u: user cpu time,
1.172 - /// \li \c s: system cpu time,
1.173 - /// \li \c cu: user cpu time of children,
1.174 - /// \li \c cs: system cpu time of children,
1.175 - /// \li \c real: real time.
1.176 - /// \relates TimeStamp
1.177 - inline std::ostream& operator<<(std::ostream& os,const TimeStamp &t)
1.178 + class NoTimeReport
1.179 {
1.180 - long cls = sysconf(_SC_CLK_TCK);
1.181 - os << "u: " << double(t.getTms().tms_utime)/cls <<
1.182 - "s, s: " << double(t.getTms().tms_stime)/cls <<
1.183 - "s, cu: " << double(t.getTms().tms_cutime)/cls <<
1.184 - "s, cs: " << double(t.getTms().tms_cstime)/cls <<
1.185 - "s, real: " << t.realTime() << "s";
1.186 - return os;
1.187 - }
1.188 + public:
1.189 + ///\e
1.190 + NoTimeReport(std::string title,std::ostream &os=std::cerr,bool run=true) {}
1.191 + ///\e Do nothing.
1.192 + ~NoTimeReport() {}
1.193
1.194 -
1.195 + operator TimeStamp () const { return TimeStamp(); }
1.196 + void reset() {}
1.197 + void start() {}
1.198 + void stop() {}
1.199 + void halt() {}
1.200 + int running() { return 0; }
1.201 + void restart() {}
1.202 + double userTime() const { return 0; }
1.203 + double systemTime() const { return 0; }
1.204 + double cUserTime() const { return 0; }
1.205 + double cSystemTime() const { return 0; }
1.206 + double realTime() const { return 0; }
1.207 + };
1.208 +
1.209 ///Tool to measure the running time more exactly.
1.210
1.211 ///This function calls \c f several times and returns the average