-
Notifications
You must be signed in to change notification settings - Fork 2
/
altBench.cpp
181 lines (159 loc) · 6.32 KB
/
altBench.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
/*
This file is an alternate Benchmarking file
which uses vectors to generate the random
data for benchmarking
brew install folly
gcc -g -std=c11 -msse4 -O2 -DNDEBUG -c h8.c &&
g++ -g -std=c++17 -msse4 -O2 -DNDEBUG -lfollybenchmark h8.o altBench.cpp
*/
#include <cstddef>
#include <cstdint>
#include <stdlib.h>
#include <iterator>
#include <limits>
#include <boost/iterator/counting_iterator.hpp>
#include <boost/iterator/transform_iterator.hpp>
#include <boost/numeric/conversion/cast.hpp>
#include "H8.hpp"
#include "Heap8.hpp"
#include "StdMinHeap.hpp"
#include <folly/Benchmark.h>
using boost::iterators::counting_iterator;
using boost::iterators::transform_iterator;
using namespace folly;
namespace {
template<class CountType, class FunctionType>
auto iter(CountType n, FunctionType f) -> decltype(auto) {
return transform_iterator<FunctionType, counting_iterator<CountType>>(n, f);
}
template<class Appendable>
void fill(Appendable& out, typename Appendable::size_type sz, bool ascending) {
typedef typename Appendable::size_type size_type;
typedef typename Appendable::value_type value_type;
value_type max = std::numeric_limits<value_type>::max();
value_type min = std::numeric_limits<value_type>::min();
double mult = (max + 1.0) / sz;
auto f = [=](size_type i) { return boost::numeric_cast<value_type>(i * mult); };
std::vector<value_type> randomVector;
for (int i = 0; i < sz; ++i) {
ascending ? randomVector.push_back(f(i)) : randomVector.push_back(rand() % ((max - min) + min));
}
auto begin = randomVector.begin();
auto end = randomVector.end();
// auto begin = iter(size_type(0), f);
// auto end = begin + sz;
out.clear();
if (ascending) {
out.append(begin, end);
} else {
out.append(std::reverse_iterator(end), std::reverse_iterator(begin));
}
}
// vector with added append() method
struct AppendableVector : public std::vector<uint16_t> {
template<class InputIterator>
void append(InputIterator from, InputIterator to) {
insert(end(), from, to);
}
};
template<class Heap>
void heapify(uint32_t n, size_t sz, bool ascending) {
typedef typename Heap::value_type value_type;
Heap h;
value_type x = 0;
for (int i = 0; i < n; ++i) {
BENCHMARK_SUSPEND {
fill(h, sz, ascending);
}
h.heapify();
x ^= h.top();
}
doNotOptimizeAway(x);
}
template<class Heap>
void heapsort(uint32_t n, size_t sz, bool ascending) {
typedef typename Heap::value_type value_type;
Heap h;
std::vector<value_type> result(sz);
for (int i = 0; i < n; ++i) {
BENCHMARK_SUSPEND {
fill(h, sz, ascending);
}
h.heapify();
for (size_t j = 0; j < sz; ++j) result[j] = h.pop();
}
doNotOptimizeAway(result[n % sz]);
}
void sort(uint32_t n, size_t sz, bool ascending) {
typedef typename AppendableVector::value_type value_type;
AppendableVector result;
for (int i = 0; i < n; ++i) {
BENCHMARK_SUSPEND {
fill(result, sz, ascending);
}
std::sort(result.begin(), result.end());
}
doNotOptimizeAway(result[n % sz]);
}
void heapify_h8_sorted(uint32_t n, size_t sz) { heapify<H8>(n, sz, true); }
void heapify_h8_unsorted(uint32_t n, size_t sz) { heapify<H8>(n, sz, false); }
void heapify_heap8_sorted(uint32_t n, size_t sz) { heapify<h8::Heap8>(n, sz, true); }
void heapify_heap8_unsorted(uint32_t n, size_t sz) { heapify<h8::Heap8>(n, sz, false); }
void heapify_std_sorted(uint32_t n, size_t sz) { heapify<StdMinHeap>(n, sz, true); }
void heapify_std_unsorted(uint32_t n, size_t sz) { heapify<StdMinHeap>(n, sz, false); }
void heapsort_h8_sorted(uint32_t n, size_t sz) { heapsort<H8>(n, sz, true); }
void heapsort_h8_unsorted(uint32_t n, size_t sz) { heapsort<H8>(n, sz, false); }
void heapsort_heap8_sorted(uint32_t n, size_t sz) { heapsort<h8::Heap8>(n, sz, true); }
void heapsort_heap8_unsorted(uint32_t n, size_t sz) { heapsort<h8::Heap8>(n, sz, false); }
void heapsort_std_sorted(uint32_t n, size_t sz) { heapsort<StdMinHeap>(n, sz, true); }
void heapsort_std_unsorted(uint32_t n, size_t sz) { heapsort<StdMinHeap>(n, sz, false); }
void sort_sorted(uint32_t n, size_t sz) { sort(n, sz, true); }
void sort_unsorted(uint32_t n, size_t sz) { sort(n, sz, false); }
} // namespace
BENCHMARK_PARAM(heapify_h8_sorted, 1000)
BENCHMARK_RELATIVE_PARAM(heapify_heap8_sorted, 1000)
BENCHMARK_RELATIVE_PARAM(heapify_std_sorted, 1000)
BENCHMARK_PARAM(heapify_h8_sorted, 100000)
BENCHMARK_RELATIVE_PARAM(heapify_heap8_sorted, 100000)
BENCHMARK_RELATIVE_PARAM(heapify_std_sorted, 100000)
BENCHMARK_PARAM(heapify_h8_sorted, 10000000)
BENCHMARK_RELATIVE_PARAM(heapify_heap8_sorted, 10000000)
BENCHMARK_RELATIVE_PARAM(heapify_std_sorted, 10000000)
BENCHMARK_PARAM(heapify_h8_unsorted, 1000)
BENCHMARK_RELATIVE_PARAM(heapify_heap8_unsorted, 1000)
BENCHMARK_RELATIVE_PARAM(heapify_std_unsorted, 1000)
BENCHMARK_PARAM(heapify_h8_unsorted, 100000)
BENCHMARK_RELATIVE_PARAM(heapify_heap8_unsorted, 100000)
BENCHMARK_RELATIVE_PARAM(heapify_std_unsorted, 100000)
BENCHMARK_PARAM(heapify_h8_unsorted, 10000000)
BENCHMARK_RELATIVE_PARAM(heapify_heap8_unsorted, 10000000)
BENCHMARK_RELATIVE_PARAM(heapify_std_unsorted, 10000000)
BENCHMARK_DRAW_LINE();
BENCHMARK_PARAM(heapsort_h8_sorted, 1000)
BENCHMARK_RELATIVE_PARAM(heapsort_heap8_sorted, 1000)
BENCHMARK_RELATIVE_PARAM(heapsort_std_sorted, 1000)
BENCHMARK_RELATIVE_PARAM(sort_sorted, 1000)
BENCHMARK_PARAM(heapsort_h8_sorted, 100000)
BENCHMARK_RELATIVE_PARAM(heapsort_heap8_sorted, 100000)
BENCHMARK_RELATIVE_PARAM(heapsort_std_sorted, 100000)
BENCHMARK_RELATIVE_PARAM(sort_sorted, 100000)
BENCHMARK_PARAM(heapsort_h8_sorted, 10000000)
BENCHMARK_RELATIVE_PARAM(heapsort_heap8_sorted, 10000000)
BENCHMARK_RELATIVE_PARAM(heapsort_std_sorted, 10000000)
BENCHMARK_RELATIVE_PARAM(sort_sorted, 10000000)
BENCHMARK_PARAM(heapsort_h8_unsorted, 1000)
BENCHMARK_RELATIVE_PARAM(heapsort_heap8_unsorted, 1000)
BENCHMARK_RELATIVE_PARAM(heapsort_std_unsorted, 1000)
BENCHMARK_RELATIVE_PARAM(sort_unsorted, 1000)
BENCHMARK_PARAM(heapsort_h8_unsorted, 100000)
BENCHMARK_RELATIVE_PARAM(heapsort_heap8_unsorted, 100000)
BENCHMARK_RELATIVE_PARAM(heapsort_std_unsorted, 100000)
BENCHMARK_RELATIVE_PARAM(sort_unsorted, 100000)
BENCHMARK_PARAM(heapsort_h8_unsorted, 10000000)
BENCHMARK_RELATIVE_PARAM(heapsort_heap8_unsorted, 10000000)
BENCHMARK_RELATIVE_PARAM(heapsort_std_unsorted, 10000000)
BENCHMARK_RELATIVE_PARAM(sort_unsorted, 10000000)
int main(int argc, char** argv) {
runBenchmarks();
return 0;
}