added precision for double output

This commit is contained in:
Robin Müller 2020-04-08 19:05:21 +02:00
parent 3dbf353385
commit da9bb97b23
2 changed files with 22 additions and 8 deletions

View File

@ -6,10 +6,11 @@
#include <framework/timemanager/Stopwatch.h>
#include <framework/serviceinterface/ServiceInterfaceStream.h>
#include <iomanip>
Stopwatch::Stopwatch(bool displayOnDestruction,
StopwatchDisplayMode displayMode):
displayOnDestruction(displayOnDestruction) {
StopwatchDisplayMode displayMode, uint8_t precision):
displayOnDestruction(displayOnDestruction), outputPrecision(precision) {
// Measures start time on initialization.
Clock::getUptime(&startTime);
}
@ -31,8 +32,9 @@ ms_double_t Stopwatch::stopPrecise() {
void Stopwatch::display() {
if(displayMode == StopwatchDisplayMode::MS_DOUBLE) {
info << "Stopwatch: Operation took " <<
elapsedTimeMsDouble << " milliseconds" << std::endl;
info << "Stopwatch: Operation took "
<< std::setprecision(outputPrecision) << elapsedTimeMsDouble
<< " milliseconds" << std::endl;
}
else {
info << "Stopwatch: Operation took " << elapsedTime.tv_sec * 1000 +
@ -56,6 +58,8 @@ StopwatchDisplayMode Stopwatch::getDisplayMode() const {
return displayMode;
}
void Stopwatch::stopInternal() {
elapsedTime = Clock::getUptime() - startTime;
elapsedTimeMsDouble = timevalOperations::toDouble(elapsedTime) * 1000.0;

View File

@ -22,9 +22,19 @@ enum class StopwatchDisplayMode {
* as a float.
*/
class Stopwatch {
public:
/**
* Default constructor. Call "Stopwatch stopwatch" without brackets if
* no parameters are required!
* @param displayOnDestruction If set to true, displays measured time on
* object destruction
* @param displayMode Display format is either MS rounded or MS as double
* format
* @param outputPrecision If using double format, specify precision here.
*/
Stopwatch(bool displayOnDestruction = true,
StopwatchDisplayMode displayMode = StopwatchDisplayMode::MS_DOUBLE);
StopwatchDisplayMode displayMode = StopwatchDisplayMode::MS_DOUBLE,
uint8_t outputPrecision = 4);
virtual~ Stopwatch();
/**
@ -53,12 +63,12 @@ class Stopwatch {
StopwatchDisplayMode getDisplayMode() const;
void setDisplayMode(StopwatchDisplayMode displayMode);
private:
timeval startTime {0, 0};
timeval elapsedTime {0, 0};
double elapsedTimeMsDouble = 0;
double elapsedTimeMsDouble {0.0};
bool displayOnDestruction = true;
uint8_t outputPrecision = 4;
StopwatchDisplayMode displayMode = StopwatchDisplayMode::MS_DOUBLE;
void stopInternal();