Changeset 1780:9f052750753f in lemon-0.x
- Timestamp:
- 11/08/05 11:12:45 (17 years ago)
- Branch:
- default
- Phase:
- public
- Convert:
- svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/lemon/trunk@2314
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
lemon/time_measure.h
r1689 r1780 67 67 const rtms &getTms() const {return ts;} 68 68 69 void _reset() 70 { ts.tms_utime=ts.tms_stime=ts.tms_cutime=ts.tms_cstime=0; real_time=0;} 71 69 72 public: 70 73 … … 81 84 /// Constructor initializing with zero 82 85 TimeStamp() 83 { ts.tms_utime=ts.tms_stime=ts.tms_cutime=ts.tms_cstime=0; real_time=0;}86 { _reset(); } 84 87 ///Constructor initializing with the current time values of the process 85 88 TimeStamp(void *) { stamp();} 86 89 90 ///Set every time value to zero 91 TimeStamp &reset() {_reset();return *this;} 92 87 93 ///\e 88 94 TimeStamp &operator+=(const TimeStamp &b) … … 118 124 } 119 125 ///\e 120 121 ///\bug operator * and / gives rounded values!122 126 TimeStamp &operator*=(double b) 123 127 { … … 181 185 return double(ts.tms_cstime)/sysconf(_SC_CLK_TCK); 182 186 } 183 ///Gives back the real time of the process187 ///Gives back the real time 184 188 double realTime() const {return real_time;} 185 189 }; … … 190 194 } 191 195 192 ///Class measuring the cpu time and real time usage of the process193 194 ///Class measuring the cpu time and real time usage of the process.196 ///Class for measuring the cpu time and real time usage of the process 197 198 ///Class for measuring the cpu time and real time usage of the process. 195 199 ///It is quite easy-to-use, here is a short example. 196 200 ///\code … … 215 219 ///\endcode 216 220 /// 221 ///The \ref Timer can also be \ref stop() "stopped" and 222 ///\ref start() "started" again, so it is easy to compute collected 223 ///running times. 224 /// 225 ///\warning Depending on the operation system and its actual configuration 226 ///the time counters have a certain (relatively big) granularity. 227 ///Therefore this tool is not appropriate to measure very short times. 228 ///Also, if you start and stop the timer very frequently, it could lead 229 ///distorted results. 230 /// 231 ///The \ref Timer also counts the number of \ref start() 232 ///executions, and is stops only after the same amount (or more) 233 ///\ref stop() "stop()"s. This can be useful e.g. to compute the running time 234 ///of recursive functions. 235 /// 217 236 ///\todo This shouldn't be Unix (Linux) specific. 218 237 /// … … 220 239 class Timer 221 240 { 222 TimeStamp start_time; 223 224 void _reset() {start_time.stamp();} 241 int running; //Timer is running iff running>0; (running>=0 always holds) 242 TimeStamp start_time; //This is the relativ start-time if the timer 243 //is running, the collected running time otherwise. 244 245 void _reset() {if(running) start_time.stamp(); else start_time.reset();} 225 246 226 247 public: 227 ///Constructor. It starts with zero time counters 228 Timer() {_reset();} 248 ///Constructor. 249 250 ///\param _running indicates whether or not the timer starts immediately. 251 /// 252 Timer(bool _running=true) :running(_running) {_reset();} 229 253 230 254 ///Computes the ellapsed time 231 255 232 256 ///This conversion computes the ellapsed time 233 ///since the construction of \c t or since 234 ///the last \c t.reset(). 257 /// 235 258 operator TimeStamp () const 236 259 { 237 260 TimeStamp t; 238 261 t.stamp(); 239 return t-start_time;262 return running?t-start_time:start_time; 240 263 } 241 264 … … 249 272 } 250 273 251 274 ///Start the time counters 275 276 ///This function starts the time counters. 277 /// 278 ///If the timer is started more than ones, it will remain running 279 ///until the same amount of \ref stop() is called. 280 ///\sa stop() 281 void start() 282 { 283 if(running) running++; 284 else { 285 TimeStamp t; 286 t.stamp(); 287 start_time=t-start_time; 288 } 289 } 290 291 ///Stop the time counters 292 293 ///This function stops the time counters. 294 /// 295 ///\sa stop() 296 void stop() 297 { 298 if(running && !--running) { 299 TimeStamp t; 300 t.stamp(); 301 start_time=t-start_time; 302 } 303 } 304 252 305 ///Gives back the ellapsed user time of the process 253 306 double userTime() const … … 270 323 return operator TimeStamp().cSystemTime(); 271 324 } 272 ///Gives back the ellapsed real time of the process325 ///Gives back the ellapsed real time 273 326 double realTime() const 274 327 { … … 307 360 ///This function calls \c f several times and returns the average 308 361 ///running time. The number of the executions will be choosen in such a way 309 ///that the full r unning time will be roughly between \c min_time362 ///that the full real running time will be roughly between \c min_time 310 363 ///and <tt>2*min_time</tt>. 311 364 ///\param f the function object to be measured.
Note: See TracChangeset
for help on using the changeset viewer.