From 02279833aa081f351fa1373dab173ff0f6025d8a Mon Sep 17 00:00:00 2001 From: hellwig Date: Tue, 2 Oct 2012 12:53:33 +0000 Subject: [PATCH] WallclockTimer updated git-svn-id: https://www4.informatik.uni-erlangen.de/i4svn/danceos/trunk/devel/fail@1708 8c4709b5-6ec9-48aa-a5cd-a96041d1645a --- src/core/util/WallclockTimer.cc | 46 ++++++++------------------------ src/core/util/WallclockTimer.hpp | 10 ++++--- 2 files changed, 18 insertions(+), 38 deletions(-) diff --git a/src/core/util/WallclockTimer.cc b/src/core/util/WallclockTimer.cc index 3e578934..7fc8af23 100644 --- a/src/core/util/WallclockTimer.cc +++ b/src/core/util/WallclockTimer.cc @@ -15,10 +15,11 @@ void WallclockTimer::startTimer() { gettimeofday(&start, NULL); } -std::string WallclockTimer::getRuntimeAsString() { +std::string WallclockTimer::getRuntimeAsString() const { int length; long t1,t2, duration; + struct timeval current; std::stringstream lengthinfo, resultstring; if (isRunning) { @@ -33,7 +34,7 @@ std::string WallclockTimer::getRuntimeAsString() { duration = t2-t1; lengthinfo << duration-((duration/1000000)*1000000); length = lengthinfo.str().length(); - resultstring << (int) duration/1000000 << "."; + resultstring << (int) duration/1000000 << ","; if (length < 6) { int i; @@ -46,37 +47,8 @@ std::string WallclockTimer::getRuntimeAsString() { return resultstring.str(); } -double WallclockTimer::getRuntimeAsDouble() { - - int length; - long t1,t2, duration; - double resultdouble; - std::stringstream lengthinfo, resultstring; - - if (isRunning) { - gettimeofday(¤t, NULL); - t1 = (start.tv_sec*1000000)+start.tv_usec; - t2 = (current.tv_sec*1000000)+current.tv_usec; - } else { - t1 = (start.tv_sec*1000000)+start.tv_usec; - t2 = (end.tv_sec*1000000)+end.tv_usec; - } - - duration = t2-t1; - lengthinfo << duration-((duration/1000000)*1000000); - length = lengthinfo.str().length(); - resultstring << (int) duration/1000000 << "."; - - if (length < 6) { - int i; - for (i = 0 ; i< 6-length ; i++){ - resultstring << "0"; - } - } - - resultstring << (int) duration-((duration/1000000)*1000000); - resultdouble = atof( resultstring.str().c_str() ); - return resultdouble; +double WallclockTimer::getRuntimeAsDouble() const { + return atof(getRuntimeAsString().c_str()); } @@ -92,10 +64,14 @@ void WallclockTimer::reset() { isRunning = false; start.tv_sec = 0; start.tv_usec = 0; - current.tv_sec = 0; - current.tv_usec = 0; end.tv_sec = 0; end.tv_usec = 0; } + +std::ostream& operator<< (std::ostream& os, const WallclockTimer& w) { + os << w.getRuntimeAsString(); + return os; +} + } // end-of-namespace: fail diff --git a/src/core/util/WallclockTimer.hpp b/src/core/util/WallclockTimer.hpp index 03f890f1..53e6bc78 100644 --- a/src/core/util/WallclockTimer.hpp +++ b/src/core/util/WallclockTimer.hpp @@ -24,7 +24,7 @@ class WallclockTimer { private: bool isRunning; - struct timeval start,end,current; + struct timeval start,end; public: WallclockTimer(); @@ -36,11 +36,11 @@ public: /** * Returns the elapsed time as string. This works while the timer is running, and if it is stopped. */ - std::string getRuntimeAsString(); + std::string getRuntimeAsString() const; /** * Returns the elapsed time as double. This works while the timer is running, and if it is stopped. */ - double getRuntimeAsDouble(); + double getRuntimeAsDouble() const; /** * Stops the timer. */ @@ -50,9 +50,13 @@ public: */ void reset(); + operator double() { return getRuntimeAsDouble(); } + operator int() { return ((int) getRuntimeAsDouble()); } }; +std::ostream& operator<< (std::ostream& os, const WallclockTimer& w); + } // end-of-namespace: fail #endif // __WALLCLOCKTIMER_HPP__