-
Notifications
You must be signed in to change notification settings - Fork 16
/
perf_benchmark.cpp
357 lines (315 loc) · 10.4 KB
/
perf_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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*
* B-heap priority queue
*
* Copyright Björn Fahller 2015
*
* Use, modification and distribution is subject to the
* Boost Software License, Version 1.0. (See accompanying
* file LICENSE_1_0.txt or copy at
* http://www.boost.org/LICENSE_1_0.txt)
*
* Project home: https://github.com/rollbear/prio_queue
*/
#include "prio_queue.hpp"
#include <tachymeter/benchmark.hpp>
#include <tachymeter/seq.hpp>
#include <tachymeter/CSV_reporter.hpp>
#include <queue>
#include <random>
#include <chrono>
#include <algorithm>
#include <iostream>
#include <memory>
#include <sstream>
using namespace std::literals::chrono_literals;
using namespace tachymeter;
using Clock = std::chrono::high_resolution_clock;
using rollbear::prio_queue;
struct null_obj_t
{
constexpr operator int() const { return 0; }
template <typename T>
constexpr operator std::unique_ptr<T>() const { return nullptr; }
};
static const constexpr null_obj_t null_obj{ };
static int n[600000];
auto const test_sizes = powers(seq(1, 2, 5), 1, 100000, 10);
auto const min_test_duration = 1000ms;
template <typename T>
struct is_pair
{
struct no {};
static no func(...);
struct yes {};
template <typename U, typename V>
static yes func(std::pair<U, V> const*);
static constexpr bool value = std::is_same<yes, decltype(func(std::declval<T*>()))>::value;
};
template <typename T, typename ... V>
struct has_push
{
struct no {};
static no func(...);
template <typename U>
static auto func(U* u) -> decltype(u->push(std::declval<V>()...));
static constexpr bool value = !std::is_same<no, decltype(func(std::declval<T*>()))>::value;
};
template <typename Q>
inline
std::enable_if_t<is_pair<typename Q::value_type>::value>
add(Q& q, int n)
{
q.push(typename Q::value_type(n, null_obj));
}
template <typename Q>
inline
std::enable_if_t<!is_pair<typename Q::value_type>::value && has_push<Q, int>::value>
add(Q& q, int n)
{
q.push(n);
}
template <typename Q>
inline
std::enable_if_t<!is_pair<typename Q::value_type>::value && !has_push<Q, int>::value>
add(Q& q, int n)
{
q.push(n, null_obj);
}
template <typename Q>
class populate
{
public:
populate(uint64_t) { }
void operator()(uint64_t size)
{
for (uint64_t i = 0; i != size; ++i)
{
add(q, n[i]);
}
}
private:
Q q;
};
template <typename Q>
class pop_all
{
public:
pop_all(std::size_t size)
{
for (uint64_t i = 0; i != size; ++i)
{
add(q, n[i]);
}
}
void operator()(uint64_t size)
{
while (size--)
{
q.pop();
}
}
private:
Q q;
};
template <typename Q, uint64_t delta_size, uint64_t num_cycles>
class operate
{
public:
operate(std::size_t size)
{
for (uint64_t i = 0; i != size; ++i)
{
add(q, n[i]);
}
}
void operator()(uint64_t size)
{
auto p = n + size;
auto remaining_cycles = num_cycles;
while (remaining_cycles--)
{
auto elements_to_push = delta_size;
while (elements_to_push--)
{
add(q, *p++);
}
auto elements_to_pop = delta_size;
while (elements_to_pop--)
{
q.pop();
}
}
}
private:
Q q;
};
template <typename Q, uint64_t num_cycles>
class reschedule
{
public:
reschedule(std::size_t size)
{
for (uint64_t i = 0; i != size; ++i)
{
add(q, n[i]);
}
}
void operator()(uint64_t size)
{
auto p = n + size;
auto remaining_cycles = num_cycles;
while (remaining_cycles--)
{
q.reschedule_top(*p++);
}
}
private:
Q q;
};
template <typename Q, uint64_t num_cycles>
class pop_push
{
public:
pop_push(std::size_t size)
{
for (uint64_t i = 0; i != size; ++i)
{
add(q, n[i]);
}
}
void operator()(uint64_t size)
{
auto p = n + size;
auto remaining_cycles = num_cycles;
while (remaining_cycles--)
{
q.pop();
add(q, *p++);
}
}
private:
Q q;
};
inline
bool operator<(const std::pair<int, std::unique_ptr<int>> &lh,
const std::pair<int, std::unique_ptr<int>> &rh)
{
return lh.first < rh.first;
}
template <std::size_t size>
void measure_prio_queue(int argc, char *argv[])
{
std::ostringstream os;
os << "/tmp/q/" << size;
std::string path = os.str();
std::cout << path << '\n';
CSV_reporter reporter(path.c_str(), &std::cout);
benchmark<Clock> benchmark(reporter);
using qint = prio_queue<8, int, void>;
using qintintp = prio_queue<size, std::pair<int, int>, void>;
using qintptrp = prio_queue<size, std::pair<int, std::unique_ptr<int>>, void>;
using qintp = prio_queue<size, int, std::unique_ptr<int>>;
using qintint = prio_queue<size, int, int>;
using std::to_string;
benchmark.measure<populate<qint>>(test_sizes,
"populate prio_queue<int,void>",
min_test_duration);
benchmark.measure<pop_all<qint>>(test_sizes,
"pop all prio_queue<int,void>",
min_test_duration);
benchmark.measure<operate<qint, 320, 200>>(test_sizes,
"operate prio_queue<int,void>",
min_test_duration);
benchmark.measure<populate<qintintp>>(test_sizes,
"populate prio_queue<<int,int>, void>",
min_test_duration);
benchmark.measure<pop_all<qintintp>>(test_sizes,
"pop all prio_queue<<int,int>, void>",
min_test_duration);
benchmark.measure<operate<qintintp, 320, 200>>(test_sizes,
"operate prio_queue<<int,int>, void>",
min_test_duration);
benchmark.measure<populate<qintptrp>>(test_sizes,
"populate prio_queue<<int,ptr>, void>",
min_test_duration);
benchmark.measure<pop_all<qintptrp>>(test_sizes,
"pop all prio_queue<<int,ptr>, void>",
min_test_duration);
benchmark.measure<operate<qintptrp, 320, 200>>(test_sizes,
"operate prio_queue<<int,ptr>, void>",
min_test_duration);
benchmark.measure<populate<qintint>>(test_sizes,
"populate prio_queue<int,int>",
min_test_duration);
benchmark.measure<pop_all<qintint>>(test_sizes,
"pop all prio_queue<int,int>",
min_test_duration);
benchmark.measure<operate<qintint, 320, 200>>(test_sizes,
"operate prio_queue<int,int>",
min_test_duration);
benchmark.measure<reschedule<qintint, 1000>>(test_sizes,
"reschedule prio_queue<int,int>",
min_test_duration);
benchmark.measure<pop_push<qintint, 1000>>(test_sizes,
"reschedule with pop/push prio_queue<int,int>",
min_test_duration);
benchmark.measure<populate<qintp>>(test_sizes,
"populate prio_queue<int,ptr>",
min_test_duration);
benchmark.measure<pop_all<qintp>>(test_sizes,
"pop all prio_queue<int,ptr>",
min_test_duration);
benchmark.measure<operate<qintp, 320, 200>>(test_sizes,
"operate prio_queue<int,ptr>",
min_test_duration);
benchmark.run(argc, argv);
}
int main(int argc, char *argv[])
{
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dist(1, 10000000);
for (auto& i : n) i = dist(gen);
std::cout << sizeof(int) << ' '
<< sizeof(std::pair<int, std::unique_ptr<int>>) << '\n';
measure_prio_queue<8>(argc, argv);
measure_prio_queue<16>(argc, argv);
measure_prio_queue<32>(argc, argv);
measure_prio_queue<64>(argc, argv);
using qint = std::priority_queue<int>;
using qintintp = std::priority_queue<std::pair<int, int>>;
using qintptrp = std::priority_queue<std::pair<int, std::unique_ptr<int>>>;
CSV_reporter reporter("/tmp/q/std", &std::cout);
benchmark<Clock> benchmark(reporter);
benchmark.measure<pop_push<qint, 1000>>(test_sizes,
"std pop/push",
min_test_duration);
benchmark.measure<populate<qint>>(test_sizes,
"populate priority_queue<int>",
min_test_duration);
benchmark.measure<pop_all<qint>>(test_sizes,
"pop all priority_queue<int>",
min_test_duration);
benchmark.measure<operate<qint, 320, 200>>(test_sizes,
"operate priority_queue<int>",
min_test_duration);
benchmark.measure<populate<qintintp>>(test_sizes,
"populate priority_queue<<int,int>>",
min_test_duration);
benchmark.measure<pop_all<qintintp>>(test_sizes,
"pop all priority_queue<<int,int>>",
min_test_duration);
benchmark.measure<operate<qintintp, 320, 200>>(test_sizes,
"operate priority_queue<<int,int>>",
min_test_duration);
benchmark.measure<populate<qintptrp>>(test_sizes,
"populate priority_queue<<int,ptr>>",
min_test_duration);
benchmark.measure<pop_all<qintptrp>>(test_sizes,
"pop all priority_queue<<int,ptr>>",
min_test_duration);
benchmark.measure<operate<qintptrp, 320, 200>>(test_sizes,
"operate priority_queue<<int,ptr>>",
min_test_duration);
benchmark.run(argc, argv);
}