/** * @file Stopwatch.cpp * * @date 08.04.2020 */ #include #include #include Stopwatch::Stopwatch(bool displayOnDestruction, StopwatchDisplayMode displayMode): displayOnDestruction(displayOnDestruction) { // Measures start time on initialization. Clock::getUptime(&startTime); } 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) { info << "Stopwatch: Operation took " << elapsedTime.tv_sec * 1000 + elapsedTime.tv_usec * 1000 << " milliseconds"; } else if(displayMode == StopwatchDisplayMode::SECONDS) { info <<"Stopwatch: Operation took " << std::setprecision(4) << std::fixed << timevalOperations::toDouble(elapsedTime) << " seconds"; } } 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; }