2020-04-08 19:33:01 +02:00
|
|
|
#include <framework/timemanager/Stopwatch.h>
|
|
|
|
#include <framework/serviceinterface/ServiceInterfaceStream.h>
|
|
|
|
#include <iomanip>
|
|
|
|
|
|
|
|
Stopwatch::Stopwatch(bool displayOnDestruction,
|
|
|
|
StopwatchDisplayMode displayMode): displayMode(displayMode),
|
|
|
|
displayOnDestruction(displayOnDestruction) {
|
|
|
|
// Measures start time on initialization.
|
2020-04-09 18:02:42 +02:00
|
|
|
startTime = Clock::getUptime();
|
2020-04-08 19:33:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Stopwatch::start() {
|
|
|
|
startTime = Clock::getUptime();
|
|
|
|
}
|
|
|
|
|
|
|
|
millis_t Stopwatch::stop() {
|
|
|
|
stopInternal();
|
|
|
|
return elapsedTime.tv_sec * 1000 + elapsedTime.tv_usec / 1000;
|
|
|
|
}
|
|
|
|
|
|
|
|
seconds_t Stopwatch::stopSeconds() {
|
|
|
|
stopInternal();
|
|
|
|
return timevalOperations::toDouble(elapsedTime);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Stopwatch::display() {
|
|
|
|
if(displayMode == StopwatchDisplayMode::MILLIS) {
|
2020-05-25 15:25:17 +02:00
|
|
|
sif::info << "Stopwatch: Operation took " << (elapsedTime.tv_sec * 1000 +
|
2020-04-09 18:02:42 +02:00
|
|
|
elapsedTime.tv_usec / 1000) << " milliseconds" << std::endl;
|
2020-04-08 19:33:01 +02:00
|
|
|
}
|
|
|
|
else if(displayMode == StopwatchDisplayMode::SECONDS) {
|
2020-05-25 15:25:17 +02:00
|
|
|
sif::info <<"Stopwatch: Operation took " << std::setprecision(3)
|
2020-04-08 19:33:01 +02:00
|
|
|
<< std::fixed << timevalOperations::toDouble(elapsedTime)
|
|
|
|
<< " seconds" << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Stopwatch::~Stopwatch() {
|
|
|
|
if(displayOnDestruction) {
|
|
|
|
stopInternal();
|
|
|
|
display();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Stopwatch::setDisplayMode(StopwatchDisplayMode displayMode) {
|
|
|
|
this->displayMode = displayMode;
|
|
|
|
}
|
|
|
|
|
|
|
|
StopwatchDisplayMode Stopwatch::getDisplayMode() const {
|
|
|
|
return displayMode;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Stopwatch::stopInternal() {
|
|
|
|
elapsedTime = Clock::getUptime() - startTime;
|
|
|
|
}
|