stopwatch more lightweight now

This commit is contained in:
Robin Müller 2020-04-08 18:27:18 +02:00
parent e0e1e64a09
commit 3af241b9c4
2 changed files with 21 additions and 31 deletions

View File

@ -10,7 +10,6 @@
Stopwatch::Stopwatch(bool displayOnDestruction, DisplayMode displayMode):
displayOnDestruction(displayOnDestruction) {
Clock::getUptime(&startTime);
stopwatchState = StopwatchState::STARTED;
}
void Stopwatch::start() {
@ -18,31 +17,22 @@ void Stopwatch::start() {
}
ms_normal_t Stopwatch::stop() {
elapsedTime = Clock::getUptime() - startTime;
int32_t elapsedTimeMs = elapsedTime.tv_sec * 1000 +
elapsedTime.tv_usec/1000;
if(elapsedTimeMs < 0) {
error << "Stopwatch: Measured time negative!";
return INVALID_TIME;
}
else {
return static_cast<ms_normal_t>(elapsedTimeMs);
}
stopInternal();
return elapsedTime.tv_sec * 1000 + elapsedTime.tv_usec / 1000;
}
ms_double_t Stopwatch::stopPrecise() {
elapsedTime = Clock::getUptime() - startTime;
return timevalOperations::toDouble(elapsedTime) * 1000.0;
stopInternal();
return elapsedTimeMsDouble;
}
void Stopwatch::display() {
if(displayMode == DisplayMode::MS_FLOAT) {
if(displayMode == DisplayMode::MS_DOUBLE) {
info << "Stopwatch: Operation took " <<
elapsedTimeMs << " milliseconds" << std::endl;
elapsedTimeMsDouble << " milliseconds" << std::endl;
}
else {
timeval elapsedTime = stopTime - startTime;
info << "Stopwatch: Operation took " << elapsedTime.tv_sec * 1000 +
elapsedTime.tv_usec * 1000 << " milliseconds";
}
@ -56,7 +46,8 @@ Stopwatch::~Stopwatch() {
}
}
void Stopwatch::stopInternal() {
elapsedTime = Clock::getUptime() - startTime;
elapsedTimeMs = timevalOperations::toDouble(elapsedTime) * 1000.0;
elapsedTimeMsDouble = timevalOperations::toDouble(elapsedTime) * 1000.0;
}

View File

@ -8,14 +8,23 @@
#define FRAMEWORK_TIMEMANAGER_STOPWATCH_H_
#include <framework/timemanager/Clock.h>
/**
* @brief Simple Stopwatch implementation to measure elapsed time
* @details
* This class can be used to measure elapsed times. It also displays elapsed
* times automatically on destruction if not explicitely deactivated in the
* constructor. The default time format is the elapsed time in miliseconds
* as a float.
* This class caches the value
*/
class Stopwatch {
enum class DisplayMode {
MS_FLOAT,
MS_DOUBLE,
MS
};
Stopwatch(bool displayOnDestruction = true, DisplayMode displayMode =
DisplayMode::MS_FLOAT);
DisplayMode::MS_DOUBLE);
virtual~ Stopwatch();
void start();
@ -25,23 +34,13 @@ class Stopwatch {
void display();
private:
static const ms_normal_t INVALID_TIME = 0xFFFFFFFF;
timeval startTime {0, 0};
timeval stopTime {0, 0};
timeval elapsedTime {0, 0};
double elapsedTimeMs = 0;
//ms_normal_t elapsedTimeMs {0};
double elapsedTimeMsDouble = 0;
bool displayOnDestruction = true;
enum class StopwatchState {
IDLE,
STARTED,
};
StopwatchState stopwatchState = StopwatchState::IDLE;
DisplayMode displayMode = DisplayMode::MS_FLOAT;
DisplayMode displayMode = DisplayMode::MS_DOUBLE;
void stopInternal();
};