# HG changeset patch # User Alpar Juttner # Date 1338890788 -7200 # Node ID c40a9d94442d7143e733b38869ad98bf1f082b0a # Parent 38e1d4383262a146b119291383c65007f7704ec3 New features in time_measure.h (#442) diff -r 38e1d4383262 -r c40a9d94442d lemon/base.cc --- a/lemon/base.cc Sun May 06 17:18:39 2012 +0200 +++ b/lemon/base.cc Tue Jun 05 12:06:28 2012 +0200 @@ -21,6 +21,7 @@ #include #include +#include namespace lemon { float Tolerance::def_epsilon = static_cast(1e-4); @@ -31,4 +32,6 @@ const Invalid INVALID = Invalid(); #endif + TimeStamp::Format TimeStamp::_format = TimeStamp::NORMAL; + } //namespace lemon diff -r 38e1d4383262 -r c40a9d94442d lemon/math.h --- a/lemon/math.h Sun May 06 17:18:39 2012 +0200 +++ b/lemon/math.h Tue Jun 05 12:06:28 2012 +0200 @@ -65,8 +65,13 @@ return v!=v; } + ///Round a value to its closest integer + inline double round(double r) { + return (r > 0.0) ? floor(r + 0.5) : ceil(r - 0.5); + } + /// @} } //namespace lemon -#endif //LEMON_TOLERANCE_H +#endif //LEMON_MATH_H diff -r 38e1d4383262 -r c40a9d94442d lemon/time_measure.h --- a/lemon/time_measure.h Sun May 06 17:18:39 2012 +0200 +++ b/lemon/time_measure.h Tue Jun 05 12:06:28 2012 +0200 @@ -34,6 +34,7 @@ #include #include #include +#include namespace lemon { @@ -63,12 +64,41 @@ double cstime; double rtime; + public: + ///Display format specifier + + ///\e + /// + enum Format { + /// Reports all measured values + NORMAL = 0, + /// Only real time and an error indicator is displayed + SHORT = 1 + }; + + private: + static Format _format; + void _reset() { utime = stime = cutime = cstime = rtime = 0; } public: + ///Set output format + + ///Set output format. + /// + ///The output format is global for all timestamp instances. + static void format(Format f) { _format = f; } + ///Retrieve the current output format + + ///Retrieve the current output format + /// + ///The output format is global for all timestamp instances. + static Format format() { return _format; } + + ///Read the current time values of the process void stamp() { @@ -224,11 +254,24 @@ /// calculated. inline std::ostream& operator<<(std::ostream& os,const TimeStamp &t) { - os << "u: " << t.userTime() << - "s, s: " << t.systemTime() << - "s, cu: " << t.cUserTime() << - "s, cs: " << t.cSystemTime() << - "s, real: " << t.realTime() << "s"; + switch(t._format) + { + case TimeStamp::NORMAL: + os << "u: " << t.userTime() << + "s, s: " << t.systemTime() << + "s, cu: " << t.cUserTime() << + "s, cs: " << t.cSystemTime() << + "s, real: " << t.realTime() << "s"; + break; + case TimeStamp::SHORT: + double total = t.userTime()+t.systemTime()+ + t.cUserTime()+t.cSystemTime(); + os << t.realTime() + << "s (err: " << round((t.realTime()-total)/ + t.realTime()*10000)/100 + << "%)"; + break; + } return os; } @@ -468,6 +511,7 @@ { std::string _title; std::ostream &_os; + bool _active; public: ///Constructor @@ -475,13 +519,27 @@ ///\param title This text will be printed before the ellapsed time. ///\param os The stream to print the report to. ///\param run Sets whether the timer should start immediately. - TimeReport(std::string title,std::ostream &os=std::cerr,bool run=true) - : Timer(run), _title(title), _os(os){} + ///\param active Sets whether the report should actually be printed + /// on destruction. + TimeReport(std::string title,std::ostream &os=std::cerr,bool run=true, + bool active=true) + : Timer(run), _title(title), _os(os), _active(active) {} ///Destructor that prints the ellapsed time ~TimeReport() { - _os << _title << *this << std::endl; + if(_active) _os << _title << *this << std::endl; } + + ///Retrieve the activity status + + ///\e + /// + bool active() const { return _active; } + ///Set the activity status + + /// This function set whether the time report should actually be printed + /// on destruction. + void active(bool a) { _active=a; } }; ///'Do nothing' version of TimeReport