1 /* -*- C++ -*- |
1 /* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 * |
2 * |
3 * This file is a part of LEMON, a generic C++ optimization library |
3 * This file is a part of LEMON, a generic C++ optimization library. |
4 * |
4 * |
5 * Copyright (C) 2003-2008 |
5 * Copyright (C) 2003-2008 |
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 * (Egervary Research Group on Combinatorial Optimization, EGRES). |
7 * (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 * |
8 * |
94 GetSystemTimeAsFileTime(&system); |
94 GetSystemTimeAsFileTime(&system); |
95 rtime = ch * system.dwHighDateTime + cl * system.dwLowDateTime; |
95 rtime = ch * system.dwHighDateTime + cl * system.dwLowDateTime; |
96 |
96 |
97 FILETIME create, exit, kernel, user; |
97 FILETIME create, exit, kernel, user; |
98 if (GetProcessTimes(GetCurrentProcess(),&create, &exit, &kernel, &user)) { |
98 if (GetProcessTimes(GetCurrentProcess(),&create, &exit, &kernel, &user)) { |
99 utime = ch * user.dwHighDateTime + cl * user.dwLowDateTime; |
99 utime = ch * user.dwHighDateTime + cl * user.dwLowDateTime; |
100 stime = ch * kernel.dwHighDateTime + cl * kernel.dwLowDateTime; |
100 stime = ch * kernel.dwHighDateTime + cl * kernel.dwLowDateTime; |
101 cutime = 0; |
101 cutime = 0; |
102 cstime = 0; |
102 cstime = 0; |
103 } else { |
103 } else { |
104 rtime = 0; |
104 rtime = 0; |
105 utime = 0; |
105 utime = 0; |
106 stime = 0; |
106 stime = 0; |
107 cutime = 0; |
107 cutime = 0; |
108 cstime = 0; |
108 cstime = 0; |
109 } |
109 } |
110 #endif |
110 #endif |
111 } |
111 } |
112 |
112 |
113 /// Constructor initializing with zero |
113 /// Constructor initializing with zero |
114 TimeStamp() |
114 TimeStamp() |
115 { _reset(); } |
115 { _reset(); } |
116 ///Constructor initializing with the current time values of the process |
116 ///Constructor initializing with the current time values of the process |
117 TimeStamp(void *) { stamp();} |
117 TimeStamp(void *) { stamp();} |
118 |
118 |
119 ///Set every time value to zero |
119 ///Set every time value to zero |
120 TimeStamp &reset() {_reset();return *this;} |
120 TimeStamp &reset() {_reset();return *this;} |
121 |
121 |
122 ///\e |
122 ///\e |
123 TimeStamp &operator+=(const TimeStamp &b) |
123 TimeStamp &operator+=(const TimeStamp &b) |
203 { |
203 { |
204 return stime; |
204 return stime; |
205 } |
205 } |
206 ///Gives back the user time of the process' children |
206 ///Gives back the user time of the process' children |
207 |
207 |
208 ///\note On <tt>WIN32</tt> platform this value is not calculated. |
208 ///\note On <tt>WIN32</tt> platform this value is not calculated. |
209 /// |
209 /// |
210 double cUserTime() const |
210 double cUserTime() const |
211 { |
211 { |
212 return cutime; |
212 return cutime; |
213 } |
213 } |
214 ///Gives back the user time of the process' children |
214 ///Gives back the user time of the process' children |
215 |
215 |
216 ///\note On <tt>WIN32</tt> platform this value is not calculated. |
216 ///\note On <tt>WIN32</tt> platform this value is not calculated. |
217 /// |
217 /// |
218 double cSystemTime() const |
218 double cSystemTime() const |
219 { |
219 { |
220 return cstime; |
220 return cstime; |
221 } |
221 } |
222 ///Gives back the real time |
222 ///Gives back the real time |
223 double realTime() const {return rtime;} |
223 double realTime() const {return rtime;} |
224 }; |
224 }; |
225 |
225 |
226 TimeStamp operator*(double b,const TimeStamp &t) |
226 TimeStamp operator*(double b,const TimeStamp &t) |
227 { |
227 { |
228 return t*b; |
228 return t*b; |
229 } |
229 } |
230 |
230 |
231 ///Prints the time counters |
231 ///Prints the time counters |
232 |
232 |
233 ///Prints the time counters in the following form: |
233 ///Prints the time counters in the following form: |
234 /// |
234 /// |
235 /// <tt>u: XX.XXs s: XX.XXs cu: XX.XXs cs: XX.XXs real: XX.XXs</tt> |
235 /// <tt>u: XX.XXs s: XX.XXs cu: XX.XXs cs: XX.XXs real: XX.XXs</tt> |
297 class Timer |
297 class Timer |
298 { |
298 { |
299 int _running; //Timer is running iff _running>0; (_running>=0 always holds) |
299 int _running; //Timer is running iff _running>0; (_running>=0 always holds) |
300 TimeStamp start_time; //This is the relativ start-time if the timer |
300 TimeStamp start_time; //This is the relativ start-time if the timer |
301 //is _running, the collected _running time otherwise. |
301 //is _running, the collected _running time otherwise. |
302 |
302 |
303 void _reset() {if(_running) start_time.stamp(); else start_time.reset();} |
303 void _reset() {if(_running) start_time.stamp(); else start_time.reset();} |
304 |
304 |
305 public: |
305 public: |
306 ///Constructor. |
306 ///Constructor. |
307 |
307 |
308 ///\param run indicates whether or not the timer starts immediately. |
308 ///\param run indicates whether or not the timer starts immediately. |
309 /// |
309 /// |
310 Timer(bool run=true) :_running(run) {_reset();} |
310 Timer(bool run=true) :_running(run) {_reset();} |
329 _running=0; |
329 _running=0; |
330 _reset(); |
330 _reset(); |
331 } |
331 } |
332 |
332 |
333 ///Start the time counters |
333 ///Start the time counters |
334 |
334 |
335 ///This function starts the time counters. |
335 ///This function starts the time counters. |
336 /// |
336 /// |
337 ///If the timer is started more than ones, it will remain running |
337 ///If the timer is started more than ones, it will remain running |
338 ///until the same amount of \ref stop() is called. |
338 ///until the same amount of \ref stop() is called. |
339 ///\sa stop() |
339 ///\sa stop() |
340 void start() |
340 void start() |
341 { |
341 { |
342 if(_running) _running++; |
342 if(_running) _running++; |
343 else { |
343 else { |
344 _running=1; |
344 _running=1; |
345 TimeStamp t; |
345 TimeStamp t; |
346 t.stamp(); |
346 t.stamp(); |
347 start_time=t-start_time; |
347 start_time=t-start_time; |
348 } |
348 } |
349 } |
349 } |
350 |
350 |
351 |
351 |
352 ///Stop the time counters |
352 ///Stop the time counters |
353 |
353 |
354 ///This function stops the time counters. If start() was executed more than |
354 ///This function stops the time counters. If start() was executed more than |
355 ///once, then the same number of stop() execution is necessary the really |
355 ///once, then the same number of stop() execution is necessary the really |
356 ///stop the timer. |
356 ///stop the timer. |
357 /// |
357 /// |
358 ///\sa halt() |
358 ///\sa halt() |
359 ///\sa start() |
359 ///\sa start() |
360 ///\sa restart() |
360 ///\sa restart() |
361 ///\sa reset() |
361 ///\sa reset() |
362 |
362 |
363 void stop() |
363 void stop() |
364 { |
364 { |
365 if(_running && !--_running) { |
365 if(_running && !--_running) { |
366 TimeStamp t; |
366 TimeStamp t; |
367 t.stamp(); |
367 t.stamp(); |
368 start_time=t-start_time; |
368 start_time=t-start_time; |
369 } |
369 } |
370 } |
370 } |
371 |
371 |
372 ///Halt (i.e stop immediately) the time counters |
372 ///Halt (i.e stop immediately) the time counters |
373 |
373 |
400 ///For example the timer |
400 ///For example the timer |
401 ///is running if and only if the return value is \c true |
401 ///is running if and only if the return value is \c true |
402 ///(i.e. greater than |
402 ///(i.e. greater than |
403 ///zero). |
403 ///zero). |
404 int running() { return _running; } |
404 int running() { return _running; } |
405 |
405 |
406 |
406 |
407 ///Restart the time counters |
407 ///Restart the time counters |
408 |
408 |
409 ///This function is a shorthand for |
409 ///This function is a shorthand for |
410 ///a reset() and a start() calls. |
410 ///a reset() and a start() calls. |
411 /// |
411 /// |
412 void restart() |
412 void restart() |
413 { |
413 { |
414 reset(); |
414 reset(); |
415 start(); |
415 start(); |
416 } |
416 } |
417 |
417 |
418 ///@} |
418 ///@} |
419 |
419 |
420 ///\name Query Functions for the ellapsed time |
420 ///\name Query Functions for the ellapsed time |
421 |
421 |
422 ///@{ |
422 ///@{ |
431 { |
431 { |
432 return operator TimeStamp().systemTime(); |
432 return operator TimeStamp().systemTime(); |
433 } |
433 } |
434 ///Gives back the ellapsed user time of the process' children |
434 ///Gives back the ellapsed user time of the process' children |
435 |
435 |
436 ///\note On <tt>WIN32</tt> platform this value is not calculated. |
436 ///\note On <tt>WIN32</tt> platform this value is not calculated. |
437 /// |
437 /// |
438 double cUserTime() const |
438 double cUserTime() const |
439 { |
439 { |
440 return operator TimeStamp().cUserTime(); |
440 return operator TimeStamp().cUserTime(); |
441 } |
441 } |
442 ///Gives back the ellapsed user time of the process' children |
442 ///Gives back the ellapsed user time of the process' children |
443 |
443 |
444 ///\note On <tt>WIN32</tt> platform this value is not calculated. |
444 ///\note On <tt>WIN32</tt> platform this value is not calculated. |
445 /// |
445 /// |
446 double cSystemTime() const |
446 double cSystemTime() const |
447 { |
447 { |
448 return operator TimeStamp().cSystemTime(); |
448 return operator TimeStamp().cSystemTime(); |
449 } |
449 } |
485 ///\endcode |
485 ///\endcode |
486 /// |
486 /// |
487 ///\sa Timer |
487 ///\sa Timer |
488 ///\sa NoTimeReport |
488 ///\sa NoTimeReport |
489 ///\todo There is no test case for this |
489 ///\todo There is no test case for this |
490 class TimeReport : public Timer |
490 class TimeReport : public Timer |
491 { |
491 { |
492 std::string _title; |
492 std::string _title; |
493 std::ostream &_os; |
493 std::ostream &_os; |
494 public: |
494 public: |
495 ///\e |
495 ///\e |
496 |
496 |
497 ///\param title This text will be printed before the ellapsed time. |
497 ///\param title This text will be printed before the ellapsed time. |
498 ///\param os The stream to print the report to. |
498 ///\param os The stream to print the report to. |
499 ///\param run Sets whether the timer should start immediately. |
499 ///\param run Sets whether the timer should start immediately. |
500 |
500 |
501 TimeReport(std::string title,std::ostream &os=std::cerr,bool run=true) |
501 TimeReport(std::string title,std::ostream &os=std::cerr,bool run=true) |
502 : Timer(run), _title(title), _os(os){} |
502 : Timer(run), _title(title), _os(os){} |
503 ///\e Prints the ellapsed time on destruction. |
503 ///\e Prints the ellapsed time on destruction. |
504 ~TimeReport() |
504 ~TimeReport() |
505 { |
505 { |
506 _os << _title << *this << std::endl; |
506 _os << _title << *this << std::endl; |
507 } |
507 } |
508 }; |
508 }; |
509 |
509 |
510 ///'Do nothing' version of \ref TimeReport |
510 ///'Do nothing' version of \ref TimeReport |
511 |
511 |
512 ///\sa TimeReport |
512 ///\sa TimeReport |
513 /// |
513 /// |
514 class NoTimeReport |
514 class NoTimeReport |
525 |
525 |
526 operator TimeStamp () const { return TimeStamp(); } |
526 operator TimeStamp () const { return TimeStamp(); } |
527 void reset() {} |
527 void reset() {} |
528 void start() {} |
528 void start() {} |
529 void stop() {} |
529 void stop() {} |
530 void halt() {} |
530 void halt() {} |
531 int running() { return 0; } |
531 int running() { return 0; } |
532 void restart() {} |
532 void restart() {} |
533 double userTime() const { return 0; } |
533 double userTime() const { return 0; } |
534 double systemTime() const { return 0; } |
534 double systemTime() const { return 0; } |
535 double cUserTime() const { return 0; } |
535 double cUserTime() const { return 0; } |
536 double cSystemTime() const { return 0; } |
536 double cSystemTime() const { return 0; } |
537 double realTime() const { return 0; } |
537 double realTime() const { return 0; } |
538 }; |
538 }; |
539 |
539 |
540 ///Tool to measure the running time more exactly. |
540 ///Tool to measure the running time more exactly. |
541 |
541 |
542 ///This function calls \c f several times and returns the average |
542 ///This function calls \c f several times and returns the average |
543 ///running time. The number of the executions will be choosen in such a way |
543 ///running time. The number of the executions will be choosen in such a way |
544 ///that the full real running time will be roughly between \c min_time |
544 ///that the full real running time will be roughly between \c min_time |
545 ///and <tt>2*min_time</tt>. |
545 ///and <tt>2*min_time</tt>. |
546 ///\param f the function object to be measured. |
546 ///\param f the function object to be measured. |
548 ///\retval num if it is not \c NULL, then the actual |
548 ///\retval num if it is not \c NULL, then the actual |
549 /// number of execution of \c f will be written into <tt>*num</tt>. |
549 /// number of execution of \c f will be written into <tt>*num</tt>. |
550 ///\retval full_time if it is not \c NULL, then the actual |
550 ///\retval full_time if it is not \c NULL, then the actual |
551 /// total running time will be written into <tt>*full_time</tt>. |
551 /// total running time will be written into <tt>*full_time</tt>. |
552 ///\return The average running time of \c f. |
552 ///\return The average running time of \c f. |
553 |
553 |
554 template<class F> |
554 template<class F> |
555 TimeStamp runningTimeTest(F f,double min_time=10,unsigned int *num = NULL, |
555 TimeStamp runningTimeTest(F f,double min_time=10,unsigned int *num = NULL, |
556 TimeStamp *full_time=NULL) |
556 TimeStamp *full_time=NULL) |
557 { |
557 { |
558 TimeStamp full; |
558 TimeStamp full; |