2020-08-18 13:09:15 +02:00
|
|
|
#include "../timemanager/Stopwatch.h"
|
|
|
|
#include "../serviceinterface/ServiceInterfaceStream.h"
|
2020-04-08 19:05:21 +02:00
|
|
|
#include <iomanip>
|
2020-04-08 18:08:14 +02:00
|
|
|
|
2020-04-08 18:33:38 +02:00
|
|
|
Stopwatch::Stopwatch(bool displayOnDestruction,
|
2020-04-23 22:05:12 +02:00
|
|
|
StopwatchDisplayMode displayMode): displayOnDestruction(
|
|
|
|
displayOnDestruction), displayMode(displayMode) {
|
2020-04-08 18:33:38 +02:00
|
|
|
// Measures start time on initialization.
|
2020-05-29 02:23:14 +02:00
|
|
|
Clock::getClock_timeval(&startTime);
|
2020-04-08 18:08:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Stopwatch::start() {
|
2020-05-29 02:23:14 +02:00
|
|
|
Clock::getClock_timeval(&startTime);
|
2020-04-08 18:08:14 +02:00
|
|
|
}
|
|
|
|
|
2020-06-06 23:23:20 +02:00
|
|
|
dur_millis_t Stopwatch::stop() {
|
2020-04-08 18:27:18 +02:00
|
|
|
stopInternal();
|
|
|
|
return elapsedTime.tv_sec * 1000 + elapsedTime.tv_usec / 1000;
|
2020-04-08 18:08:14 +02:00
|
|
|
}
|
|
|
|
|
2020-06-06 23:23:20 +02:00
|
|
|
dur_seconds_t Stopwatch::stopSeconds() {
|
2020-04-08 18:27:18 +02:00
|
|
|
stopInternal();
|
2020-04-08 19:24:24 +02:00
|
|
|
return timevalOperations::toDouble(elapsedTime);
|
2020-04-08 18:08:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Stopwatch::display() {
|
2020-04-08 19:24:24 +02:00
|
|
|
if(displayMode == StopwatchDisplayMode::MILLIS) {
|
2020-04-23 21:59:24 +02:00
|
|
|
sif::info << "Stopwatch: Operation took " << (elapsedTime.tv_sec * 1000 +
|
2020-04-09 18:02:10 +02:00
|
|
|
elapsedTime.tv_usec / 1000) << " milliseconds" << std::endl;
|
2020-04-08 18:08:14 +02:00
|
|
|
}
|
2020-04-08 19:24:24 +02:00
|
|
|
else if(displayMode == StopwatchDisplayMode::SECONDS) {
|
2020-04-23 21:59:24 +02:00
|
|
|
sif::info <<"Stopwatch: Operation took " << std::setprecision(3)
|
2020-04-08 19:24:24 +02:00
|
|
|
<< std::fixed << timevalOperations::toDouble(elapsedTime)
|
2020-04-08 19:30:39 +02:00
|
|
|
<< " seconds" << std::endl;
|
2020-04-08 19:24:24 +02:00
|
|
|
}
|
2020-04-08 18:08:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Stopwatch::~Stopwatch() {
|
|
|
|
if(displayOnDestruction) {
|
|
|
|
stopInternal();
|
|
|
|
display();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-08 18:33:38 +02:00
|
|
|
void Stopwatch::setDisplayMode(StopwatchDisplayMode displayMode) {
|
|
|
|
this->displayMode = displayMode;
|
|
|
|
}
|
|
|
|
|
|
|
|
StopwatchDisplayMode Stopwatch::getDisplayMode() const {
|
|
|
|
return displayMode;
|
|
|
|
}
|
2020-04-08 18:27:18 +02:00
|
|
|
|
2020-04-08 18:08:14 +02:00
|
|
|
void Stopwatch::stopInternal() {
|
2020-05-29 02:23:14 +02:00
|
|
|
timeval endTime;
|
|
|
|
Clock::getClock_timeval(&endTime);
|
|
|
|
elapsedTime = endTime - startTime;
|
2020-04-08 18:08:14 +02:00
|
|
|
}
|