[Lemon-commits] Alpar Juttner: New features in time_measure.h (#...
Lemon HG
hg at lemon.cs.elte.hu
Tue Mar 19 15:42:33 CET 2013
details: http://lemon.cs.elte.hu/hg/lemon/rev/c40a9d94442d
changeset: 1222:c40a9d94442d
user: Alpar Juttner <alpar [at] cs.elte.hu>
date: Tue Jun 05 12:06:28 2012 +0200
description:
New features in time_measure.h (#442)
diffstat:
lemon/base.cc | 3 ++
lemon/math.h | 7 ++++-
lemon/time_measure.h | 74 ++++++++++++++++++++++++++++++++++++++++++++++-----
3 files changed, 75 insertions(+), 9 deletions(-)
diffs (158 lines):
diff --git a/lemon/base.cc b/lemon/base.cc
--- a/lemon/base.cc
+++ b/lemon/base.cc
@@ -21,6 +21,7 @@
#include<lemon/tolerance.h>
#include<lemon/core.h>
+#include<lemon/time_measure.h>
namespace lemon {
float Tolerance<float>::def_epsilon = static_cast<float>(1e-4);
@@ -31,4 +32,6 @@
const Invalid INVALID = Invalid();
#endif
+ TimeStamp::Format TimeStamp::_format = TimeStamp::NORMAL;
+
} //namespace lemon
diff --git a/lemon/math.h b/lemon/math.h
--- a/lemon/math.h
+++ b/lemon/math.h
@@ -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 --git a/lemon/time_measure.h b/lemon/time_measure.h
--- a/lemon/time_measure.h
+++ b/lemon/time_measure.h
@@ -34,6 +34,7 @@
#include <string>
#include <fstream>
#include <iostream>
+#include <lemon/math.h>
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
More information about the Lemon-commits
mailing list