-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmarker.hpp
128 lines (116 loc) · 4.17 KB
/
benchmarker.hpp
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
#pragma once
#include <iostream>
#include <chrono>
#include <map>
#include <unordered_map>
#include <vector>
#include <string>
/*
The purpose of this class is to measure the performance of random
pieces of code and map those to a string, to later produce a summary
of execution time for every string. For example
Benchmark::start_one("foo");
{
// code to benchmark
}
Benchmark::end_one("foo");
...
Benchmarker::print_summary(std::cout);
*/
using duration_t = long long;
class Benchmarker {
private:
// map key -> vector of durations
static std::unordered_map<std::string, std::vector<std::chrono::nanoseconds>> durations;
static std::unordered_map<std::string, std::chrono::time_point<std::chrono::steady_clock>> pending_starts;
static void print_duration(std::ostream &os, duration_t duration) {
if (duration < 10000ll) {
// nanoseconds
os << duration << "ns";
return;
}
duration_t duration_us = duration / 1000ll;
if (duration_us < 10000ll) {
// microseconds
os << duration_us << "us";
return;
}
duration_t duration_ms = duration_us / 1000ll;
if (duration_ms < 10000ll) {
// milliseconds
os << duration_ms << "ms";
return;
}
duration_t duration_s = duration_ms / 1000ll;
// seconds
os << duration_s << "s";
}
static std::unordered_map<std::string, duration_t> compute_totals() {
std::unordered_map<std::string, duration_t> totals;
for (auto& [key, durations] : durations) {
duration_t total = 0;
for (auto& duration : durations) {
total += duration.count();
}
totals[key] = total;
}
return totals;
}
static std::unordered_map<std::string, duration_t> compute_counts() {
std::unordered_map<std::string, duration_t> counts;
for (auto& [key, durations] : durations) {
counts[key] = durations.size();
}
return counts;
}
static std::unordered_map<std::string, std::pair<duration_t, duration_t>> compute_min_max() {
std::unordered_map<std::string, std::pair<duration_t, duration_t>> min_maxes;
for (auto& [key, durations] : durations) {
duration_t min = durations[0].count();
duration_t max = durations[0].count();
for (auto& duration : durations) {
if (duration.count() < min) {
min = duration.count();
}
if (duration.count() > max) {
max = duration.count();
}
}
min_maxes[key] = std::make_pair(min, max);
}
return min_maxes;
}
public:
static void print_summary(std::ostream& os) {
std::unordered_map<std::string, duration_t> totals = compute_totals();
std::unordered_map<std::string, duration_t> counts = compute_counts();
std::unordered_map<std::string, std::pair<duration_t, duration_t>> min_maxes = compute_min_max();
for (auto& [key, total] : totals) {
os << key << ": ";
duration_t avg = total / counts[key];
print_duration(os, total);
os << " (" << counts[key] << " runs, min: ";
print_duration(os, min_maxes[key].first);
os << ", max: ";
print_duration(os, min_maxes[key].second);
os << ", avg: ";
print_duration(os, avg);
os << ")\n";
}
}
static void start_one(const std::string& key) {
pending_starts[key] = std::chrono::steady_clock::now();
}
static void end_one(const std::string& key) {
auto start = pending_starts[key];
auto end = std::chrono::steady_clock::now();
durations[key].push_back(end - start);
pending_starts.erase(key);
}
static void clear() {
durations.clear();
pending_starts.clear();
}
};
std::unordered_map<std::string, std::chrono::time_point<std::chrono::steady_clock>> Benchmarker::pending_starts;
std::unordered_map<std::string, std::vector<std::chrono::nanoseconds>> Benchmarker::durations;