#ifndef SOPHIAR2_STATISTIC_TIMER_HPP #define SOPHIAR2_STATISTIC_TIMER_HPP #include "core/timestamp_helper.hpp" #include #include #include #include #include #include #include #include #include #include namespace sophiar { struct statistic_timer_single_shot_tag; struct statistic_timer_start_stop_tag; template class statistic_timer { public: void start(timestamp_type current_ts = current_timestamp()); template std::enable_if_t> stop(timestamp_type current_ts = current_timestamp()) { assert(is_running); is_running = false; accumulator(current_ts - last_ts); } void reset() { accumulator = {}; } auto get_count() const { return boost::accumulators::count(accumulator); } auto get_min() const { return boost::accumulators::min(accumulator); } auto get_max() const { return boost::accumulators::max(accumulator); } auto get_mean() const { return boost::accumulators::mean(accumulator); } auto get_std() const { return std::sqrt(boost::accumulators::variance(accumulator)); } std::string to_string() const; private: using float_type = double; using accumulator_type = boost::accumulators::accumulator_set< float_type, boost::accumulators::stats< boost::accumulators::tag::min, boost::accumulators::tag::max, boost::accumulators::tag::variance> >; accumulator_type accumulator; bool is_running = false; timestamp_type last_ts = 0; }; template<> void statistic_timer::start(timestamp_type current_ts) { if (last_ts != 0) { accumulator(current_ts - last_ts); } last_ts = current_ts; } template<> void statistic_timer::start(timestamp_type current_ts) { assert(!is_running); is_running = true; last_ts = current_ts; } template<> std::string statistic_timer::to_string() const { return fmt::format("Cnt: {}, " "Min: {:.3f}ms({:.3f}Hz), Max: {:.3f}ms({:.3f}Hz), " "Mean: {:.3f}({:.3f})ms({:.3f}Hz)", get_count(), get_min() / 1e3, 1e6 / get_min(), get_max() / 1e3, 1e6 / get_max(), get_mean() / 1e3, get_std() / 1e3, 1e6 / get_mean()); } template<> std::string statistic_timer::to_string() const { return fmt::format("Cnt: {}, Min: {:.3f}ms, Max: {:.3f}ms, Mean: {:.3f}({:.3f})ms", get_count(), get_min() / 1e3, get_max() / 1e3, get_mean() / 1e3, get_std() / 1e3); } } #endif //SOPHIAR2_STATISTIC_TIMER_HPP