-
Notifications
You must be signed in to change notification settings - Fork 0
/
gbench_sort.cpp
63 lines (48 loc) · 1.48 KB
/
gbench_sort.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
// comparing sort algorithms with Google Benchmark
// Author: Jeff Trull <edaskel@att.net>
#include <benchmark/benchmark.h>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <random>
#include <iostream>
#include <iterator>
int
qs_comparator( void const * a, void const * b ) {
// sort in *decreasing* order by reversing comparison
return *reinterpret_cast<int const *>(b) - *reinterpret_cast<int const *>(a);
}
void qs_sorter( std::vector<int> v ) {
qsort(v.data(), v.size(), sizeof(int), qs_comparator);
benchmark::DoNotOptimize(v);
}
auto std_comparator = [](int a, int b) { return b < a; }; // reversed
void std_sorter( std::vector<int> v ) {
sort(v.begin(), v.end(), std_comparator);
benchmark::DoNotOptimize(v);
}
template<typename Sorter>
void BM_sort(Sorter s, benchmark::State & state) {
using namespace std;
const size_t testsize = state.range(0);
random_device rnd_device;
mt19937 mersenne_engine(rnd_device());
uniform_int_distribution<int> dist;
// construct random vector of supplied size
vector<int> rands(testsize);
generate_n(rands.begin(),
testsize,
[&](){ return dist(mersenne_engine); });
for (auto _ : state) {
s(rands);
}
}
void BM_qsort(benchmark::State & state) {
BM_sort(qs_sorter, state);
}
void BM_stdsort(benchmark::State & state) {
BM_sort(std_sorter, state);
}
BENCHMARK(BM_qsort)->Arg(10000);
BENCHMARK(BM_stdsort)->Arg(10000);
BENCHMARK_MAIN()