CARLA
 
载入中...
搜索中...
未找到
SnippetProfiler.h
浏览该文件的文档.
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
13namespace carla {
14namespace traffic_manager {
15
16namespace chr = std::chrono;
17using namespace chr;
18// 定义一个时间点类型,以系统时钟和纳秒精度表示
19using TimePoint = chr::time_point<chr::system_clock, chr::nanoseconds>;
20
21// This class can be used to measure execution time, total call duration and number of calls
22// of any code snippet by assigning it a name.
24
25private:
26// 存储打印时间点的哈希表,键为代码片段名称,值为时间点
27 std::unordered_map<std::string, TimePoint> print_clocks;
28 // 存储代码片段不同时间点的哈希表
29 std::unordered_map<std::string, TimePoint> snippet_clocks;
30 std::unordered_map<std::string, chr::duration<float>> snippet_durations;
31 std::unordered_map<std::string, unsigned long> number_of_calls;
32
33public:
35// 测量代码片段的执行时间
36 void MeasureExecutionTime(std::string snippet_name, bool begin_or_end) {
37 TimePoint current_time = chr::system_clock::now();
38 // 如果打印时间点哈希表中没有该代码片段的记录,则插入当前时间点
39 if (print_clocks.find(snippet_name) == print_clocks.end()) {
40 print_clocks.insert({snippet_name, current_time});
41 }
42 // 如果代码片段开始时间点哈希表中没有该代码片段的记录,则插入当前时间点
43 if (snippet_clocks.find(snippet_name) == snippet_clocks.end()) {
44 snippet_clocks.insert({snippet_name, current_time});
45 }
46 // 如果代码片段持续时间哈希表中没有该代码片段的记录,则插入 0 持续时间
47 if (snippet_durations.find(snippet_name) == snippet_durations.end()) {
48 snippet_durations.insert({snippet_name, chr::duration<float>()});
49 }
50 // 如果代码片段调用次数哈希表中没有该代码片段的记录,则插入 0 次调用
51 if (number_of_calls.find(snippet_name) == number_of_calls.end()) {
52 number_of_calls.insert({snippet_name, 0u});
53 }
54// 获取代码片段的打印时间点、开始时间点、持续时间和调用次数的引用
55 TimePoint &print_clock = print_clocks.at(snippet_name);
56 TimePoint &snippet_clock = snippet_clocks.at(snippet_name);
57 chr::duration<float> &snippet_duration = snippet_durations.at(snippet_name);
58 unsigned long &call_count = number_of_calls.at(snippet_name);
59// 如果参数 begin_or_end 为 true,表示开始测量,将当前时间点设置为代码片段开始时间点
60 if (begin_or_end) {
61 snippet_clock = current_time;
62 } else {
63 chr::duration<float> measured_duration = current_time - snippet_clock;
64 snippet_duration += measured_duration;
65 ++call_count;
66 }
67
68 chr::duration<float> print_duration = current_time - print_clock;
69 if (print_duration.count() > 1.0f) {
70 // 如果调用次数为 0,则设置为 1,避免除以 0 的错误
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 << ", "
76 << std::endl;
77// 重置总持续时间和调用次数
78 snippet_duration = 0s;
79 call_count = 0u;
80// 更新打印时间点
81 print_clock = current_time;
82 }
83 }
84};
85
86} // namespace traffic_manager
87} // namespace carla
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
时间点类型,使用系统时钟和纳秒精度
CARLA模拟器的主命名空间。
Definition Carla.cpp:139