-
Notifications
You must be signed in to change notification settings - Fork 14
/
util.hpp
196 lines (160 loc) · 5.33 KB
/
util.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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#pragma once
#include <benchmark/benchmark.h>
#include <algorithm>
#include <random>
#include <vector>
#include <mutex>
#include <atomic>
#include <thread>
#include <condition_variable>
#include <iostream>
#include <iomanip>
#ifndef COURSE_GLOBAL_ARGS
# define COURSE_GLOBAL_ARGS()
#endif
#define BENCH(func, test_case_name, ...) \
BENCHMARK_PRIVATE_DECLARE(func) = \
(::benchmark::internal::RegisterBenchmarkInternal( \
new ::benchmark::internal::FunctionBenchmark( \
#test_case_name, \
[](::benchmark::State& st) { \
FlushCacheNonpausing(); \
func(st, __VA_ARGS__); \
} \
) \
)) \
COURSE_GLOBAL_ARGS()
inline void FlushCacheNonpausing(int level = 3) {
const auto& cpu = benchmark::CPUInfo::Get();
std::vector<unsigned> resetter;
for (const auto& cache : cpu.caches) {
if (cache.level != level) {
continue;
}
resetter.resize(cache.size / sizeof(unsigned),
static_cast<unsigned>(level));
}
if (resetter.empty()) {
throw std::logic_error("Failed to reset cache");
}
unsigned trash = static_cast<unsigned>(level * level);
for (unsigned& v : resetter) {
trash += v;
v += trash;
}
benchmark::DoNotOptimize(trash);
benchmark::DoNotOptimize(resetter);
}
template <class T>
inline void fill_container_impl(T& c, std::size_t size) {
std::minstd_rand e1;
std::uniform_int_distribution<int> uniform_dist(-100000, 100000);
c.clear();
for (std::size_t i = 0; i < size / 2; ++i) {
c.insert(c.end(), uniform_dist(e1));
c.insert(c.end(), uniform_dist(e1));
}
}
template <class T>
inline void fill_container(T& c, std::size_t size) {
fill_container_impl(c, size);
}
inline void fill_container(std::vector<int>& c, std::size_t size) {
c.reserve(size);
fill_container_impl(c, size);
}
// Fills container with Range elements count, measures `f(d)` execution time, compares result with `ethalon(d)`.
template <class F, class Ethalon>
inline void algorithms(benchmark::State& state, F f, Ethalon ethalon) {
std::vector<int> d;
for (auto _ : state) {
state.PauseTiming();
fill_container(d, state.range(0));
state.ResumeTiming();
const auto val = f(d); // Measuring this call
benchmark::DoNotOptimize(val);
state.PauseTiming();
if (f != ethalon) {
const auto ethalon_value = ethalon(d);
if (ethalon_value != val) {
throw std::logic_error("Values missmatch");
}
}
state.ResumeTiming();
}
state.SetComplexityN(state.range(0));
}
template <class Type>
static void iteration(benchmark::State& state, const Type& /*container_type*/) {
for (auto _ : state) {
state.PauseTiming();
Type d;
fill_container(d, state.range(0));
state.ResumeTiming();
int sum = 0;
for (auto v: d) {
sum += v; // Measuring this call
}
benchmark::DoNotOptimize(sum);
}
}
template <class Type>
static void iteration_cold_cache(benchmark::State& state, const Type& /*container_type*/) {
for (auto _ : state) {
state.PauseTiming();
Type d;
fill_container(d, state.range(0));
FlushCacheNonpausing();
state.ResumeTiming();
int sum = 0;
for (auto v: d) {
sum += v; // Measuring this call
}
benchmark::DoNotOptimize(sum);
}
}
template <class Type>
static void insertion(benchmark::State& state, const Type& /*container_type*/) {
std::minstd_rand e1;
std::uniform_int_distribution<int> uniform_dist(-100000, 100000);
const std::size_t iterations_count = state.range(0);
for (auto _ : state) {
Type d;
d.clear();
for (std::size_t i = 0; i < iterations_count; ++i) {
d.insert(d.end(), uniform_dist(e1));
}
benchmark::DoNotOptimize(d);
}
}
template <class Type>
static void insertion_assoc(benchmark::State& state, const Type& /*container_type*/) {
std::minstd_rand e1;
std::uniform_int_distribution<int> uniform_dist(-100000, 100000);
const std::size_t iterations_count = state.range(0);
for (auto _ : state) {
Type d;
d.clear();
for (std::size_t i = 0; i < iterations_count; ++i) {
d.insert(uniform_dist(e1));
}
benchmark::DoNotOptimize(d);
}
}
template <class Type>
static void search_assoc(benchmark::State& state, const Type& /*container_type*/) {
Type d;
std::minstd_rand e1;
std::uniform_int_distribution<int> uniform_dist(-100000, 100000);
d.clear();
const std::size_t iterations_count = state.range(0);
for (std::size_t i = 0; i < iterations_count; ++i) {
d.insert(uniform_dist(e1));
}
for (auto _ : state) {
for (auto it = d.begin(); it != d.end(); ++it) {
auto v = *d.find(*it);
benchmark::DoNotOptimize(v);
}
}
}