15 #ifndef OPENSCENARIO_INTERPRETER__UTILITY__EXECUTION_TIMER_HPP_
16 #define OPENSCENARIO_INTERPRETER__UTILITY__EXECUTION_TIMER_HPP_
21 #include <unordered_map>
25 inline namespace utility
27 template <
typename Clock = std::chrono::system_clock>
32 std::int64_t ns_max = 0;
34 std::int64_t ns_min = std::numeric_limits<std::int64_t>::max();
36 std::int64_t ns_sum = 0;
38 std::int64_t ns_square_sum = 0;
43 template <
typename Duration>
44 auto add(Duration diff) ->
void
46 std::int64_t diff_ns = std::chrono::duration_cast<std::chrono::nanoseconds>(diff).count();
48 ns_max = std::max(ns_max, diff_ns);
49 ns_min = std::min(ns_max, diff_ns);
51 ns_square_sum += std::pow(diff_ns, 2);
57 return std::chrono::duration_cast<T>(std::chrono::nanoseconds(ns_max));
63 return std::chrono::duration_cast<T>(std::chrono::nanoseconds(ns_min));
69 return std::chrono::duration_cast<T>(std::chrono::nanoseconds(ns_sum / count));
73 auto standardDeviation()
const
75 std::int64_t mean_of_square = ns_square_sum / count;
76 std::int64_t square_of_mean = std::pow(ns_sum / count, 2);
77 std::int64_t var = mean_of_square - square_of_mean;
78 double standard_deviation = std::sqrt(var);
79 return std::chrono::duration_cast<T>(
80 std::chrono::nanoseconds(
static_cast<std::int64_t
>(standard_deviation)));
83 friend auto operator<<(std::ostream & os,
const Statistics & statistics) -> std::ostream &
85 using namespace std::chrono;
87 return os <<
"mean = " << statistics.template mean<milliseconds>().count() <<
" ms, "
88 <<
"max = " << statistics.template max<milliseconds>().count() <<
" ms, "
89 <<
"standard deviation = "
90 << statistics.template standardDeviation<milliseconds>().count() / 1000.0 <<
" ms";
94 std::unordered_map<std::string, Statistics> statistics_map;
97 template <
typename Thunk,
typename... Ts>
100 const auto begin = Clock::now();
104 const auto end = Clock::now();
106 statistics_map[tag].add(
end -
begin);
111 auto clear() { statistics_map.clear(); }
115 auto begin()
const {
return statistics_map.begin(); }
117 auto end()
const {
return statistics_map.end(); }
Definition: execution_timer.hpp:29
auto end() const
Definition: execution_timer.hpp:117
auto invoke(const std::string &tag, Thunk &&thunk)
Definition: execution_timer.hpp:98
auto clear()
Definition: execution_timer.hpp:111
auto getStatistics(const std::string &tag) -> const auto &
Definition: execution_timer.hpp:113
auto begin() const
Definition: execution_timer.hpp:115
decltype(auto) operator<<(std::basic_ostream< Ts... > &os, const AttributeHighlighter &highlight)
Definition: highlighter.hpp:39
std::string string
Definition: junit5.hpp:26