Browse Source

Use Stopwatch for measuring total time

tempestpy_adaptions
Matthias Volk 8 years ago
parent
commit
d2e7de7067
  1. 7
      src/storm/storm.cpp
  2. 121
      src/storm/utility/Stopwatch.h

7
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<storm::settings::modules::ResourceSettings>().isPrintTimeAndMemorySet()) {
storm::cli::showTimeAndMemoryStatistics(std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count());
storm::cli::showTimeAndMemoryStatistics(totalTimer.getTimeMilliseconds());
}
return 0;
} catch (storm::exceptions::BaseException const& exception) {

121
src/storm/utility/Stopwatch.h

@ -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;
};
}
}

Loading…
Cancel
Save