11#include <unordered_map>
14namespace traffic_manager {
16namespace chr = std::chrono;
19using TimePoint = chr::time_point<chr::system_clock, chr::nanoseconds>;
37 TimePoint current_time = chr::system_clock::now();
61 snippet_clock = current_time;
63 chr::duration<float> measured_duration = current_time - snippet_clock;
64 snippet_duration += measured_duration;
68 chr::duration<float> print_duration = current_time - print_clock;
69 if (print_duration.count() > 1.0f) {
71 call_count = call_count == 0u ? 1 : call_count;
72 std::cout <<
"Snippet name : " << snippet_name <<
", "
73 <<
"avg. duration : " << 1000 * snippet_duration.count() / call_count <<
" ms, "
74 <<
"total duration : " << snippet_duration.count() <<
" s, "
75 <<
"total calls : " << call_count <<
", "
78 snippet_duration = 0s;
81 print_clock = current_time;
void MeasureExecutionTime(std::string snippet_name, bool begin_or_end)
std::unordered_map< std::string, TimePoint > snippet_clocks
std::unordered_map< std::string, TimePoint > print_clocks
std::unordered_map< std::string, unsigned long > number_of_calls
std::unordered_map< std::string, chr::duration< float > > snippet_durations
chr::time_point< chr::system_clock, chr::nanoseconds > TimePoint
时间点类型,使用系统时钟和纳秒精度