# HG changeset patch
# User Alpar Juttner <alpar@cs.elte.hu>
# Date 1338890788 -7200
# Node ID c40a9d94442d7143e733b38869ad98bf1f082b0a
# Parent 38e1d4383262a146b119291383c65007f7704ec3
New features in time_measure.h (#442)
diff --git a/lemon/base.cc b/lemon/base.cc
a
|
b
|
|
21 | 21 | |
22 | 22 | #include<lemon/tolerance.h> |
23 | 23 | #include<lemon/core.h> |
| 24 | #include<lemon/time_measure.h> |
24 | 25 | namespace lemon { |
25 | 26 | |
26 | 27 | float Tolerance<float>::def_epsilon = static_cast<float>(1e-4); |
… |
… |
|
31 | 32 | const Invalid INVALID = Invalid(); |
32 | 33 | #endif |
33 | 34 | |
| 35 | TimeStamp::Format TimeStamp::_format = TimeStamp::NORMAL; |
| 36 | |
34 | 37 | } //namespace lemon |
diff --git a/lemon/math.h b/lemon/math.h
a
|
b
|
|
65 | 65 | return v!=v; |
66 | 66 | } |
67 | 67 | |
| 68 | ///Round a value to its closest integer |
| 69 | inline double round(double r) { |
| 70 | return (r > 0.0) ? floor(r + 0.5) : ceil(r - 0.5); |
| 71 | } |
| 72 | |
68 | 73 | /// @} |
69 | 74 | |
70 | 75 | } //namespace lemon |
71 | 76 | |
72 | | #endif //LEMON_TOLERANCE_H |
| 77 | #endif //LEMON_MATH_H |
diff --git a/lemon/time_measure.h b/lemon/time_measure.h
a
|
b
|
|
34 | 34 | #include <string> |
35 | 35 | #include <fstream> |
36 | 36 | #include <iostream> |
| 37 | #include <lemon/math.h> |
37 | 38 | |
38 | 39 | namespace lemon { |
39 | 40 | |
… |
… |
|
63 | 64 | double cstime; |
64 | 65 | double rtime; |
65 | 66 | |
| 67 | public: |
| 68 | ///Display format specifier |
| 69 | |
| 70 | ///\e |
| 71 | /// |
| 72 | enum Format { |
| 73 | /// Reports all measured values |
| 74 | NORMAL = 0, |
| 75 | /// Only real time and an error indicator is displayed |
| 76 | SHORT = 1 |
| 77 | }; |
| 78 | |
| 79 | private: |
| 80 | static Format _format; |
| 81 | |
66 | 82 | void _reset() { |
67 | 83 | utime = stime = cutime = cstime = rtime = 0; |
68 | 84 | } |
69 | 85 | |
70 | 86 | public: |
71 | 87 | |
| 88 | ///Set output format |
| 89 | |
| 90 | ///Set output format. |
| 91 | /// |
| 92 | ///The output format is global for all timestamp instances. |
| 93 | static void format(Format f) { _format = f; } |
| 94 | ///Retrieve the current output format |
| 95 | |
| 96 | ///Retrieve the current output format |
| 97 | /// |
| 98 | ///The output format is global for all timestamp instances. |
| 99 | static Format format() { return _format; } |
| 100 | |
| 101 | |
72 | 102 | ///Read the current time values of the process |
73 | 103 | void stamp() |
74 | 104 | { |
… |
… |
|
224 | 254 | /// calculated. |
225 | 255 | inline std::ostream& operator<<(std::ostream& os,const TimeStamp &t) |
226 | 256 | { |
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"; |
| 257 | switch(t._format) |
| 258 | { |
| 259 | case TimeStamp::NORMAL: |
| 260 | os << "u: " << t.userTime() << |
| 261 | "s, s: " << t.systemTime() << |
| 262 | "s, cu: " << t.cUserTime() << |
| 263 | "s, cs: " << t.cSystemTime() << |
| 264 | "s, real: " << t.realTime() << "s"; |
| 265 | break; |
| 266 | case TimeStamp::SHORT: |
| 267 | double total = t.userTime()+t.systemTime()+ |
| 268 | t.cUserTime()+t.cSystemTime(); |
| 269 | os << t.realTime() |
| 270 | << "s (err: " << round((t.realTime()-total)/ |
| 271 | t.realTime()*10000)/100 |
| 272 | << "%)"; |
| 273 | break; |
| 274 | } |
232 | 275 | return os; |
233 | 276 | } |
234 | 277 | |
… |
… |
|
468 | 511 | { |
469 | 512 | std::string _title; |
470 | 513 | std::ostream &_os; |
| 514 | bool _active; |
471 | 515 | public: |
472 | 516 | ///Constructor |
473 | 517 | |
… |
… |
|
475 | 519 | ///\param title This text will be printed before the ellapsed time. |
476 | 520 | ///\param os The stream to print the report to. |
477 | 521 | ///\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){} |
| 522 | ///\param active Sets whether the report should actually be printed |
| 523 | /// on destruction. |
| 524 | TimeReport(std::string title,std::ostream &os=std::cerr,bool run=true, |
| 525 | bool active=true) |
| 526 | : Timer(run), _title(title), _os(os), _active(active) {} |
480 | 527 | ///Destructor that prints the ellapsed time |
481 | 528 | ~TimeReport() |
482 | 529 | { |
483 | | _os << _title << *this << std::endl; |
| 530 | if(_active) _os << _title << *this << std::endl; |
484 | 531 | } |
| 532 | |
| 533 | ///Retrieve the activity status |
| 534 | |
| 535 | ///\e |
| 536 | /// |
| 537 | bool active() const { return _active; } |
| 538 | ///Set the activity status |
| 539 | |
| 540 | /// This function set whether the time report should actually be printed |
| 541 | /// on destruction. |
| 542 | void active(bool a) { _active=a; } |
485 | 543 | }; |
486 | 544 | |
487 | 545 | ///'Do nothing' version of TimeReport |