From 5b0f80509f7f8dec258d333c26766dfdf8336ccc Mon Sep 17 00:00:00 2001 From: "Robin.Mueller" Date: Wed, 8 Apr 2020 19:24:24 +0200 Subject: [PATCH] usec replaced by seconds --- timemanager/Clock.h | 4 ++-- timemanager/Stopwatch.cpp | 27 +++++++++++---------------- timemanager/Stopwatch.h | 22 +++++++--------------- 3 files changed, 20 insertions(+), 33 deletions(-) diff --git a/timemanager/Clock.h b/timemanager/Clock.h index afa9963c..5f18de3e 100644 --- a/timemanager/Clock.h +++ b/timemanager/Clock.h @@ -7,8 +7,8 @@ #include #include -typedef uint32_t ms_normal_t; -typedef double ms_double_t; +typedef uint32_t millis_t; +typedef float seconds_t; class Clock { public: diff --git a/timemanager/Stopwatch.cpp b/timemanager/Stopwatch.cpp index 1ccebc57..40b711d9 100644 --- a/timemanager/Stopwatch.cpp +++ b/timemanager/Stopwatch.cpp @@ -9,8 +9,8 @@ #include Stopwatch::Stopwatch(bool displayOnDestruction, - StopwatchDisplayMode displayMode, uint8_t precision): - displayOnDestruction(displayOnDestruction), outputPrecision(precision) { + StopwatchDisplayMode displayMode): + displayOnDestruction(displayOnDestruction) { // Measures start time on initialization. Clock::getUptime(&startTime); } @@ -19,30 +19,28 @@ void Stopwatch::start() { startTime = Clock::getUptime(); } -ms_normal_t Stopwatch::stop() { +millis_t Stopwatch::stop() { stopInternal(); return elapsedTime.tv_sec * 1000 + elapsedTime.tv_usec / 1000; } -ms_double_t Stopwatch::stopPrecise() { +seconds_t Stopwatch::stopSeconds() { stopInternal(); - return elapsedTimeMsDouble; + return timevalOperations::toDouble(elapsedTime); } - void Stopwatch::display() { - if(displayMode == StopwatchDisplayMode::MS_DOUBLE) { - info << "Stopwatch: Operation took " - << std::setprecision(outputPrecision) << elapsedTimeMsDouble - << " milliseconds" << std::endl; - } - else { + if(displayMode == StopwatchDisplayMode::MILLIS) { info << "Stopwatch: Operation took " << elapsedTime.tv_sec * 1000 + elapsedTime.tv_usec * 1000 << " milliseconds"; } + else if(displayMode == StopwatchDisplayMode::SECONDS) { + info <<"Stopwatch: Operation took " << std::setprecision(4) + << std::fixed << timevalOperations::toDouble(elapsedTime) + << " seconds"; + } } - Stopwatch::~Stopwatch() { if(displayOnDestruction) { stopInternal(); @@ -58,9 +56,6 @@ StopwatchDisplayMode Stopwatch::getDisplayMode() const { return displayMode; } - - void Stopwatch::stopInternal() { elapsedTime = Clock::getUptime() - startTime; - elapsedTimeMsDouble = timevalOperations::toDouble(elapsedTime) * 1000.0; } diff --git a/timemanager/Stopwatch.h b/timemanager/Stopwatch.h index 1e438272..aeb61265 100644 --- a/timemanager/Stopwatch.h +++ b/timemanager/Stopwatch.h @@ -9,8 +9,8 @@ #include enum class StopwatchDisplayMode { - MS_DOUBLE, - MS + MILLIS, + SECONDS }; /** @@ -32,9 +32,8 @@ public: * format * @param outputPrecision If using double format, specify precision here. */ - Stopwatch(bool displayOnDestruction = true, - StopwatchDisplayMode displayMode = StopwatchDisplayMode::MS_DOUBLE, - uint8_t outputPrecision = 4); + Stopwatch(bool displayOnDestruction = true, StopwatchDisplayMode displayMode + = StopwatchDisplayMode::MILLIS); virtual~ Stopwatch(); /** @@ -46,13 +45,8 @@ public: * Calculates the elapsed time since start and returns it * @return elapsed time in milliseconds (rounded) */ - ms_normal_t stop(); - - /** - * Calculates the elapsed time since start and returns it - * @return elapsed time in milliseconds (doulbe precision) - */ - ms_double_t stopPrecise(); + millis_t stop(); + seconds_t stopSeconds(); /** * Displays the elapsed times on the osstream, depending on internal display @@ -65,11 +59,9 @@ public: private: timeval startTime {0, 0}; timeval elapsedTime {0, 0}; - double elapsedTimeMsDouble {0.0}; bool displayOnDestruction = true; - uint8_t outputPrecision = 4; - StopwatchDisplayMode displayMode = StopwatchDisplayMode::MS_DOUBLE; + StopwatchDisplayMode displayMode = StopwatchDisplayMode::MILLIS; void stopInternal(); };