CARLA
SnippetProfiler.h
Go to the documentation of this file.
1 // Copyright (c) 2020 Computer Vision Center (CVC) at the Universitat Autonoma
2 // de Barcelona (UAB).
3 //
4 // This work is licensed under the terms of the MIT license.
5 // For a copy, see <https://opensource.org/licenses/MIT>.
6 
7 #pragma once
8 
9 #include <chrono>
10 #include <string>
11 #include <unordered_map>
12 
13 namespace carla {
14 namespace traffic_manager {
15 
16 namespace chr = std::chrono;
17 using namespace chr;
18 using TimePoint = chr::time_point<chr::system_clock, chr::nanoseconds>;
19 
20 // This class can be used to measure execution time, total call duration and number of calls
21 // of any code snippet by assigning it a name.
23 
24 private:
25  std::unordered_map<std::string, TimePoint> print_clocks;
26  std::unordered_map<std::string, TimePoint> snippet_clocks;
27  std::unordered_map<std::string, chr::duration<float>> snippet_durations;
28  std::unordered_map<std::string, unsigned long> number_of_calls;
29 
30 public:
32 
33  void MeasureExecutionTime(std::string snippet_name, bool begin_or_end) {
34  TimePoint current_time = chr::system_clock::now();
35 
36  if (print_clocks.find(snippet_name) == print_clocks.end()) {
37  print_clocks.insert({snippet_name, current_time});
38  }
39  if (snippet_clocks.find(snippet_name) == snippet_clocks.end()) {
40  snippet_clocks.insert({snippet_name, current_time});
41  }
42  if (snippet_durations.find(snippet_name) == snippet_durations.end()) {
43  snippet_durations.insert({snippet_name, chr::duration<float>()});
44  }
45  if (number_of_calls.find(snippet_name) == number_of_calls.end()) {
46  number_of_calls.insert({snippet_name, 0u});
47  }
48 
49  TimePoint &print_clock = print_clocks.at(snippet_name);
50  TimePoint &snippet_clock = snippet_clocks.at(snippet_name);
51  chr::duration<float> &snippet_duration = snippet_durations.at(snippet_name);
52  unsigned long &call_count = number_of_calls.at(snippet_name);
53 
54  if (begin_or_end) {
55  snippet_clock = current_time;
56  } else {
57  chr::duration<float> measured_duration = current_time - snippet_clock;
58  snippet_duration += measured_duration;
59  ++call_count;
60  }
61 
62  chr::duration<float> print_duration = current_time - print_clock;
63  if (print_duration.count() > 1.0f) {
64  call_count = call_count == 0u ? 1 : call_count;
65  std::cout << "Snippet name : " << snippet_name << ", "
66  << "avg. duration : " << 1000 * snippet_duration.count() / call_count << " ms, "
67  << "total duration : " << snippet_duration.count() << " s, "
68  << "total calls : " << call_count << ", "
69  << std::endl;
70 
71  snippet_duration = 0s;
72  call_count = 0u;
73 
74  print_clock = current_time;
75  }
76  }
77 };
78 
79 } // namespace traffic_manager
80 } // namespace carla
std::unordered_map< std::string, chr::duration< float > > snippet_durations
std::unordered_map< std::string, unsigned long > number_of_calls
chr::time_point< chr::system_clock, chr::nanoseconds > TimePoint
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:133
std::unordered_map< std::string, TimePoint > snippet_clocks
void MeasureExecutionTime(std::string snippet_name, bool begin_or_end)
std::unordered_map< std::string, TimePoint > print_clocks