-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.cpp
More file actions
180 lines (153 loc) · 5.56 KB
/
Copy pathtimer.cpp
File metadata and controls
180 lines (153 loc) · 5.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include <iostream>
#include <chrono>
#include <vector>
#include <map>
#include <string>
#include <iomanip>
#include <algorithm>
#include <numeric>
#include <cmath>
class Benchmarker {
private:
struct Measurement {
std::string name;
double time_ms;
};
struct BlockStats {
std::string name;
std::vector<double> times;
double min;
double max;
double mean;
int count;
bool has_stats; // True if multiple measurements
BlockStats(const std::string& n) : name(n), count(0), has_stats(false) {}
void add_measurement(double time) {
times.push_back(time);
count++;
if (count >= 2) {
has_stats = true;
min = *std::min_element(times.begin(), times.end());
max = *std::max_element(times.begin(), times.end());
mean = std::accumulate(times.begin(), times.end(), 0.0) / count;
}
}
};
std::map<std::string, BlockStats> blocks;
std::chrono::high_resolution_clock::time_point current_start;
std::string current_block_name;
bool is_measuring;
public:
Benchmarker() : is_measuring(false) {}
// Start measuring a block with a name
void start(const std::string& name) {
// If already measuring, automatically stop the previous block
if (is_measuring) {
stop();
}
current_block_name = name;
current_start = std::chrono::high_resolution_clock::now();
is_measuring = true;
// Initialize block if it doesn't exist
if (blocks.find(name) == blocks.end()) {
blocks.emplace(name, BlockStats(name));
}
}
// Stop measuring the current block
void stop() {
if (!is_measuring) {
std::cerr << "Warning: stop() called without an active measurement\n";
return;
}
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration<double, std::milli>(end - current_start);
double time_ms = duration.count();
// Add measurement to the block
try {
// .at() throws std::out_of_range if current_block_name is not found
auto& block = blocks.at(current_block_name);
block.add_measurement(time_ms);
}
catch (const std::out_of_range& e) {
// Handle the missing key scenario here
std::cerr << "Error: Block '" << current_block_name << "' not found! " << e.what() << '\n';
}
is_measuring = false;
}
// Convenience: measure a function with automatic start/stop
template<typename Func>
void measure(const std::string& name, Func func) {
start(name);
func();
stop();
}
// Print the results table
void print_results() const {
if (blocks.empty()) {
std::cout << "No measurements recorded.\n";
return;
}
// Calculate column widths
size_t name_width = 25;
for (const auto& pair : blocks) {
name_width = std::max(name_width, pair.first.length() + 2);
}
const int width = name_width + 55;
// Print header
std::cout << "\n" << std::string(width, '=') << "\n";
std::cout << "BENCHMARK RESULTS\n";
std::cout << std::string(width, '=') << "\n";
// Print column headers
std::cout << std::left << std::setw(name_width) << "Block Name";
std::cout << std::right;
std::cout << std::setw(12) << "Count";
std::cout << std::setw(12) << "Min (ms)";
std::cout << std::setw(12) << "Max (ms)";
std::cout << std::setw(12) << "Mean (ms)";
std::cout << std::setw(12) << "Status";
std::cout << "\n" << std::string(width, '-') << "\n";
// Print each block
for (const auto& pair : blocks) {
const auto& stats = pair.second;
std::cout << std::left << std::setw(name_width) << stats.name;
std::cout << std::right << std::fixed << std::setprecision(3);
std::cout << std::setw(12) << stats.count;
if (stats.has_stats) {
std::cout << std::setw(12) << stats.min;
std::cout << std::setw(12) << stats.max;
std::cout << std::setw(12) << stats.mean;
std::cout << std::setw(12) << "Multi";
} else if (stats.count == 1) {
std::cout << std::setw(12) << stats.times[0];
std::cout << std::setw(12) << "-";
std::cout << std::setw(12) << "-";
std::cout << std::setw(12) << "Single";
} else {
std::cout << std::setw(12) << "-";
std::cout << std::setw(12) << "-";
std::cout << std::setw(12) << "-";
std::cout << std::setw(12) << "Empty";
}
std::cout << "\n";
}
std::cout << std::string(width, '=') << "\n";
// Additional summary
std::cout << "\nLegend:\n";
std::cout << " Multi = Multiple measurements (min/max/mean available)\n";
std::cout << " Single = Single measurement (only the recorded time shown)\n";
std::cout << "\n";
}
// Reset all measurements
void clear() {
blocks.clear();
is_measuring = false;
}
// Get stats for a specific block
const BlockStats* get_stats(const std::string& name) const {
auto it = blocks.find(name);
if (it != blocks.end()) {
return &it->second;
}
return nullptr;
}
};