From d2e7de7067ae9d8f0dd75e9e77e2e38531761222 Mon Sep 17 00:00:00 2001 From: Matthias Volk Date: Mon, 9 Jan 2017 17:13:51 +0100 Subject: [PATCH] Use Stopwatch for measuring total time --- src/storm/storm.cpp | 7 +- src/storm/utility/Stopwatch.h | 121 +++++++++++++++++++++++----------- 2 files changed, 88 insertions(+), 40 deletions(-) diff --git a/src/storm/storm.cpp b/src/storm/storm.cpp index 60576c3c0..7ce7611dc 100644 --- a/src/storm/storm.cpp +++ b/src/storm/storm.cpp @@ -4,6 +4,7 @@ #include "storm/utility/macros.h" #include "storm/cli/cli.h" #include "storm/utility/initialize.h" +#include "storm/utility/Stopwatch.h" #include "storm/settings/SettingsManager.h" #include "storm/settings/modules/ResourceSettings.h" @@ -14,7 +15,7 @@ int main(const int argc, const char** argv) { try { - auto start = std::chrono::high_resolution_clock::now(); + storm::utility::Stopwatch totalTimer(true); storm::utility::setUp(); storm::cli::printHeader("Storm", argc, argv); storm::settings::initializeAll("Storm", "storm"); @@ -28,10 +29,10 @@ int main(const int argc, const char** argv) { // All operations have now been performed, so we clean up everything and terminate. storm::utility::cleanUp(); - auto end = std::chrono::high_resolution_clock::now(); + totalTimer.stop(); if (storm::settings::getModule().isPrintTimeAndMemorySet()) { - storm::cli::showTimeAndMemoryStatistics(std::chrono::duration_cast(end - start).count()); + storm::cli::showTimeAndMemoryStatistics(totalTimer.getTimeMilliseconds()); } return 0; } catch (storm::exceptions::BaseException const& exception) { diff --git a/src/storm/utility/Stopwatch.h b/src/storm/utility/Stopwatch.h index c0b937c24..a30b40683 100644 --- a/src/storm/utility/Stopwatch.h +++ b/src/storm/utility/Stopwatch.h @@ -2,69 +2,116 @@ #define STORM_UTILITY_STOPWATCH_H_ #include -#include #include "storm/utility/macros.h" namespace storm { namespace utility { - // A class that provides convenience operations to display run times. + + /*! + * A class that provides convenience operations to display run times. + */ class Stopwatch { public: - Stopwatch(double initialValueInSeconds = 0.0) : accumulatedSeconds(initialValueInSeconds), paused(false), startOfCurrentMeasurement(std::chrono::high_resolution_clock::now()) { - // Intentionally left empty + + /*! + * Constructor. + * + * @param startNow If true, the stopwatch starts right away. + */ + Stopwatch(bool startNow = false) : accumulatedTime(std::chrono::nanoseconds::zero()), stopped(true), startOfCurrentMeasurement(std::chrono::nanoseconds::zero()) { + if (startNow) { + start(); + } } - + + /*! + * Destructor. + */ ~Stopwatch() = default; - - double getAccumulatedSeconds() const { - if(paused) { - return accumulatedSeconds; - } else { - return accumulatedSeconds + std::chrono::duration(std::chrono::high_resolution_clock::now() - startOfCurrentMeasurement).count(); - } + + /*! + * Get measured time in seconds. + * + * @return seconds as floating point number. + */ + double getTimeSeconds() const { + return std::chrono::duration(accumulatedTime).count(); } - - void addToAccumulatedSeconds(double value) { - accumulatedSeconds += value; + + /*! + * Get measured time in milliseconds. + * + * @return Milliseconds. + */ + unsigned long long int getTimeMilliseconds() const { + return std::chrono::duration_cast(accumulatedTime).count(); } - - void pause() { - if(paused) { - STORM_LOG_WARN("Tried to pause a stopwatch that was already paused."); - } else { - accumulatedSeconds = getAccumulatedSeconds(); - paused = true; + + /*! + * Get measured time in nanoseconds. + * + * @return Nanoseconds. + */ + unsigned long long int getTimeNanoseconds() const { + return accumulatedTime.count(); + } + + /*! + * Add given time to measured time. + * + * @param timeNanoseconds Additional time in nanoseconds. + */ + void addToTime(std::chrono::nanoseconds timeNanoseconds) { + accumulatedTime += timeNanoseconds; + } + + /*! + * Stop stopwatch and add measured time to total time. + */ + void stop() { + if (stopped) { + // Assertions are only available in DEBUG build and therefore not used here. + STORM_LOG_WARN("Stopwatch is already paused."); } + stopped = true; + accumulatedTime += std::chrono::high_resolution_clock::now() - startOfCurrentMeasurement; } - - void unpause() { - if(paused) { - startOfCurrentMeasurement = std::chrono::high_resolution_clock::now(); - paused = false; - } else { - STORM_LOG_WARN("Tried to unpause a stopwatch that was not paused."); + + /*! + * Start stopwatch (again) and start measuring time. + */ + void start() { + if (!stopped) { + // Assertions are only available in DEBUG build and therefore not used here. + STORM_LOG_WARN("Stopwatch is already running."); } + stopped = false; + startOfCurrentMeasurement = std::chrono::high_resolution_clock::now(); } - // Note: Does NOT unpause if stopwatch is currently paused. + /*! + * Reset the stopwatch. Reset the measured time to zero and stop the stopwatch. + */ void reset() { - accumulatedSeconds = 0.0; - startOfCurrentMeasurement = std::chrono::high_resolution_clock::now(); + accumulatedTime = std::chrono::nanoseconds::zero(); + stopped = true; } - friend std::ostream& operator<<(std::ostream& out, Stopwatch const& sw) { - out << (round(sw.getAccumulatedSeconds()*1000)/1000); + friend std::ostream& operator<<(std::ostream& out, Stopwatch const& stopwatch) { + out << stopwatch.getTimeSeconds(); return out; } private: - double accumulatedSeconds; - bool paused; + // Total measured time + std::chrono::nanoseconds accumulatedTime; + // Flag indicating if the stopwatch is stopped right now. + bool stopped; + // Timepoint when the stopwatch was started the last time. std::chrono::high_resolution_clock::time_point startOfCurrentMeasurement; - }; } }