fsfw/timemanager/Stopwatch.cpp

54 lines
1.3 KiB
C++
Raw Normal View History

/**
* @file Stopwatch.cpp
*
* @date 08.04.2020
*/
#include <framework/timemanager/Stopwatch.h>
#include <framework/serviceinterface/ServiceInterfaceStream.h>
Stopwatch::Stopwatch(bool displayOnDestruction, DisplayMode displayMode):
displayOnDestruction(displayOnDestruction) {
Clock::getUptime(&startTime);
}
void Stopwatch::start() {
startTime = Clock::getUptime();
}
ms_normal_t Stopwatch::stop() {
2020-04-08 18:27:18 +02:00
stopInternal();
return elapsedTime.tv_sec * 1000 + elapsedTime.tv_usec / 1000;
}
ms_double_t Stopwatch::stopPrecise() {
2020-04-08 18:27:18 +02:00
stopInternal();
return elapsedTimeMsDouble;
}
void Stopwatch::display() {
2020-04-08 18:27:18 +02:00
if(displayMode == DisplayMode::MS_DOUBLE) {
info << "Stopwatch: Operation took " <<
2020-04-08 18:27:18 +02:00
elapsedTimeMsDouble << " milliseconds" << std::endl;
}
else {
info << "Stopwatch: Operation took " << elapsedTime.tv_sec * 1000 +
elapsedTime.tv_usec * 1000 << " milliseconds";
}
}
Stopwatch::~Stopwatch() {
if(displayOnDestruction) {
stopInternal();
display();
}
}
2020-04-08 18:27:18 +02:00
void Stopwatch::stopInternal() {
elapsedTime = Clock::getUptime() - startTime;
2020-04-08 18:27:18 +02:00
elapsedTimeMsDouble = timevalOperations::toDouble(elapsedTime) * 1000.0;
}