|
|
@ -2,69 +2,116 @@ |
|
|
|
#define STORM_UTILITY_STOPWATCH_H_ |
|
|
|
|
|
|
|
#include <chrono> |
|
|
|
#include <math.h> |
|
|
|
|
|
|
|
#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<double>(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<float>(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<std::chrono::milliseconds>(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; |
|
|
|
|
|
|
|
|
|
|
|
}; |
|
|
|
} |
|
|
|
} |
|
|
|