#include "timer.h" template Timer::Timer( const std::string& name, size_t size ) : size ( size ), name ( name ), TickCount ( 0 ), cb ( size ), started ( false ), max ( 0 ) { } template void Timer::start() { if (!started) { t = std::chrono::high_resolution_clock::now(); start_time = t; started = true; } } template void Timer::tick() { std::chrono::high_resolution_clock::time_point now = std::chrono::high_resolution_clock::now(); if ( started ) { std::chrono::duration diff = now - t; cb.push_back( diff.count() ); if (diff.count() > max) { max = diff.count(); } t = now; ++TickCount; } else { t = now; started = true; } } template void Timer::stop() { started = false; } template bool Timer::countdownComplete(double minTime) { std::chrono::high_resolution_clock::time_point now = std::chrono::high_resolution_clock::now(); if (started) { std::chrono::duration diff = now - start_time; if (diff.count() > minTime) { started = false; return true; } else { return false; } } else { return true; } } template const T Timer::getMinPeriod() { typename boost::circular_buffer::iterator it = std::min_element(cb.begin(), cb.end()); if (it != cb.end()) return *it; else return -1;} template const T Timer::getMaxPeriod() { typename boost::circular_buffer::iterator it = std::max_element(cb.begin(), cb.end()); if (it != cb.end()) return *it; else return -1; } template const T Timer::getMeanPeriod() { T sum = std::accumulate(cb.begin(), cb.end(), 0.0); return sum / cb.size(); } template const int Timer::getTickCount() const { return TickCount; } template const T Timer::getAllMaxPeriod() { return max; } template void Timer::clear() { max = 0; } template void Timer::show() { std::cout << "--- " << name << " ---" << std::endl; std::cout << "Number of ticks : " << TickCount << std::endl; if ( 0 != TickCount ) { std::cout << "Minimum period : " << Timer::getMinPeriod() * 1e6 << " us" << std::endl; std::cout << "Average period : " << Timer::getMeanPeriod() * 1e6 << " us" << std::endl; std::cout << "Maximum period : " << Timer::getMaxPeriod() * 1e6 << " us" << std::endl; std::cout << "Maximum period (All): " << Timer::getAllMaxPeriod() * 1e6 << " us" << std::endl; } } template class Timer;