-
Notifications
You must be signed in to change notification settings - Fork 9
/
benchmark.cpp
185 lines (170 loc) · 6.07 KB
/
benchmark.cpp
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
#include <benchmark/benchmark.h>
#include "minmax_and_dary_heap.hpp"
#include <vector>
#include <random>
#ifdef _MSC_VER
#define SKA_NOINLINE(...) __declspec(noinline) __VA_ARGS__
#else
#define SKA_NOINLINE(...) __VA_ARGS__ __attribute__((noinline))
#endif
template<typename Distribution, typename Randomness>
SKA_NOINLINE(decltype(auto)) no_inline_random_number(Distribution & distribution, Randomness & randomness)
{
return distribution(randomness);
}
void benchmark_heap_baseline(benchmark::State & state)
{
int num_items = state.range(0);
std::uniform_int_distribution<int> distribution;
std::mt19937_64 randomness(5);
while (state.KeepRunning())
{
for (int i = 0; i < num_items; ++i)
benchmark::DoNotOptimize(no_inline_random_number(distribution, randomness));
}
state.SetItemsProcessed(num_items * state.iterations());
}
BENCHMARK(benchmark_heap_baseline)->Range(4, 1024*1024*16);
void benchmark_push_minmax_heap(benchmark::State & state)
{
std::vector<int> heap;
int num_items = state.range(0);
heap.reserve(num_items);
std::uniform_int_distribution<int> distribution;
std::mt19937_64 randomness(5);
while (state.KeepRunning())
{
heap.clear();
for (int i = 0; i < num_items; ++i)
{
heap.push_back(no_inline_random_number(distribution, randomness));
push_minmax_heap(heap.begin(), heap.end());
}
benchmark::DoNotOptimize(heap.back());
}
state.SetItemsProcessed(num_items * state.iterations());
}
BENCHMARK(benchmark_push_minmax_heap)->Range(4, 1024*1024*16);
template<int D>
void benchmark_push_dary_heap(benchmark::State & state)
{
std::vector<int> heap;
int num_items = state.range(0);
heap.reserve(num_items);
std::uniform_int_distribution<int> distribution;
std::mt19937_64 randomness(5);
while (state.KeepRunning())
{
heap.clear();
for (int i = 0; i < num_items; ++i)
{
heap.push_back(no_inline_random_number(distribution, randomness));
push_dary_heap<D>(heap.begin(), heap.end());
}
benchmark::DoNotOptimize(heap.back());
}
state.SetItemsProcessed(num_items * state.iterations());
}
BENCHMARK_TEMPLATE(benchmark_push_dary_heap, 2)->Range(4, 1024*1024*16);
BENCHMARK_TEMPLATE(benchmark_push_dary_heap, 4)->Range(4, 1024*1024*16);
void benchmark_make_minmax_heap(benchmark::State & state)
{
std::vector<int> heap;
int num_items = state.range(0);
heap.reserve(num_items);
std::uniform_int_distribution<int> distribution;
std::mt19937_64 randomness(5);
while (state.KeepRunning())
{
heap.clear();
for (int i = 0; i < num_items; ++i)
heap.push_back(no_inline_random_number(distribution, randomness));
make_minmax_heap(heap.begin(), heap.end());
benchmark::DoNotOptimize(heap.back());
}
state.SetItemsProcessed(num_items * state.iterations());
}
BENCHMARK(benchmark_make_minmax_heap)->Range(4, 1024*1024*16);
template<int D>
void benchmark_make_dary_heap(benchmark::State & state)
{
std::vector<int> heap;
int num_items = state.range(0);
heap.reserve(num_items);
std::uniform_int_distribution<int> distribution;
std::mt19937_64 randomness(5);
while (state.KeepRunning())
{
heap.clear();
for (int i = 0; i < num_items; ++i)
heap.push_back(no_inline_random_number(distribution, randomness));
make_dary_heap<D>(heap.begin(), heap.end());
benchmark::DoNotOptimize(heap.back());
}
state.SetItemsProcessed(num_items * state.iterations());
}
BENCHMARK_TEMPLATE(benchmark_make_dary_heap, 2)->Range(4, 1024*1024*16);
BENCHMARK_TEMPLATE(benchmark_make_dary_heap, 4)->Range(4, 1024*1024*16);
void benchmark_pop_minmax_heap_min(benchmark::State & state)
{
std::vector<int> heap;
int num_items = state.range(0);
heap.reserve(num_items);
std::uniform_int_distribution<int> distribution;
std::mt19937_64 randomness(5);
while (state.KeepRunning())
{
heap.clear();
for (int i = 0; i < num_items; ++i)
heap.push_back(no_inline_random_number(distribution, randomness));
make_minmax_heap(heap.begin(), heap.end());
for (int i = 0; i < num_items; ++i)
pop_minmax_heap_min(heap.begin(), heap.end() - i);
benchmark::DoNotOptimize(heap.back());
}
state.SetItemsProcessed(num_items * state.iterations());
}
BENCHMARK(benchmark_pop_minmax_heap_min)->Range(4, 1024*1024*16);
void benchmark_pop_minmax_heap_max(benchmark::State & state)
{
std::vector<int> heap;
int num_items = state.range(0);
heap.reserve(num_items);
std::uniform_int_distribution<int> distribution;
std::mt19937_64 randomness(5);
while (state.KeepRunning())
{
heap.clear();
for (int i = 0; i < num_items; ++i)
heap.push_back(no_inline_random_number(distribution, randomness));
make_minmax_heap(heap.begin(), heap.end());
for (int i = 0; i < num_items; ++i)
pop_minmax_heap_max(heap.begin(), heap.end() - i);
benchmark::DoNotOptimize(heap.back());
}
state.SetItemsProcessed(num_items * state.iterations());
}
BENCHMARK(benchmark_pop_minmax_heap_max)->Range(4, 1024*1024*16);
template<int D>
void benchmark_pop_dary_heap(benchmark::State & state)
{
int num_items = state.range(0);
std::uniform_int_distribution<int> distribution;
std::mt19937_64 randomness(5);
std::vector<int> heap;
heap.reserve(num_items);
while (state.KeepRunning())
{
heap.clear();
for (int i = 0; i < num_items; ++i)
heap.push_back(no_inline_random_number(distribution, randomness));
make_dary_heap<D>(heap.begin(), heap.end());
for (int i = 0; i < num_items; ++i)
pop_dary_heap<D>(heap.begin(), heap.end() - i);
benchmark::DoNotOptimize(heap.back());
}
state.SetItemsProcessed(num_items * state.iterations());
}
BENCHMARK_TEMPLATE(benchmark_pop_dary_heap, 2)->Range(4, 1024*1024*16);
BENCHMARK_TEMPLATE(benchmark_pop_dary_heap, 4)->Range(4, 1024*1024*16);
BENCHMARK_MAIN();