Repository navigation
Expand file tree
/
Copy pathbenchmark.cpp
More file actions
172 lines (163 loc) · 6.45 KB
/
Copy pathbenchmark.cpp
File metadata and controls
172 lines (163 loc) · 6.45 KB
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
// Standalone C++11 benchmark; build with optimization and an include path to
// the header under test. Define AVL_ARRAY_SIZE_ONLY for a minimal size driver.
#include <avl_array.h>
#include <cstdint>
#ifdef AVL_ARRAY_SIZE_ONLY
template<bool Fast>
int exercise(int key)
{
avl_array<int, int, std::uint16_t, 512, Fast> tree;
for (int i = 0; i < 512; ++i) tree.insert(i ^ key, i);
int result = 0;
tree.find(key, result);
tree.insert(key, result + 1);
tree.erase(key);
for (auto it = tree.begin(); it != tree.end(); ++it) result += *it;
return result;
}
int main(int argc, char**)
{
return (exercise<true>(argc) + exercise<false>(argc)) & 255;
}
#else
#include <avl_array2.h>
#include <algorithm>
#include <array>
#include <chrono>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <string>
#include <type_traits>
namespace {
const int capacity = 512;
typedef std::array<int, capacity> order_type; // indexes of the keys in insert order
volatile std::uint64_t sink = 0;
// Keep each timed operation separate so changes to insertion cannot change
// register allocation/inlining in the lookup measurements, for example.
#if defined(_MSC_VER)
#define AVL_BENCH_NOINLINE __declspec(noinline)
#elif defined(__GNUC__) || defined(__clang__)
#define AVL_BENCH_NOINLINE __attribute__((noinline))
#else
#define AVL_BENCH_NOINLINE
#endif
// key at an index of the insert order
template<typename Key>
inline const Key& at(const std::array<Key, capacity>& keys, int i)
{
return keys[static_cast<std::size_t>(i)];
}
template<int Operation, typename Tree, typename Key>
AVL_BENCH_NOINLINE std::uint64_t run_operation(Tree& tree,
const order_type& order, const std::array<Key, capacity>& keys,
const std::array<Key, capacity>& misses)
{
std::uint64_t checksum = 0;
switch (Operation) {
case 0:
for (int i : order) checksum += tree.insert(at(keys, i), i);
break;
case 1:
for (int i : order) checksum += tree.insert(at(keys, i), i + 1);
break;
case 2:
for (int i : order) {
int value = 0;
checksum += tree.find(at(keys, i), value);
checksum += static_cast<unsigned>(value);
}
break;
case 3:
for (int i : order) {
int value = 0;
checksum += tree.find(at(misses, i), value);
}
break;
case 4:
for (int i : order) checksum += tree.erase(at(keys, i));
break;
default:
for (auto it = tree.begin(); it != tree.end(); ++it) checksum += static_cast<unsigned>(*it);
break;
}
return checksum;
}
template<typename Key> Key make_key(int value);
template<> int make_key<int>(int value) { return value; }
template<> std::string make_key<std::string>(int value)
{
// Fixed-width, longer than common small-string buffers: ordering matches int.
return std::string(40, 'k') + std::to_string(10000 + value);
}
template<typename Key, typename Tree>
void measure(const char* container, bool shuffled, int rounds)
{
typedef Tree tree_type;
order_type order;
std::array<Key, capacity> keys, misses;
for (std::size_t i = 0; i < order.size(); ++i) {
order[i] = static_cast<int>(i);
keys[i] = make_key<Key>(static_cast<int>(i));
misses[i] = make_key<Key>(static_cast<int>(i) + capacity);
}
if (shuffled) {
std::uint32_t state = 123456789U;
for (std::size_t i = order.size() - 1U; i > 0U; --i) {
state = state * 1664525U + 1013904223U;
std::swap(order[i], order[state % (i + 1U)]);
}
}
const char* names[] = { "insert", "update", "find_hit", "find_miss", "erase", "iterate" };
typedef std::uint64_t (*operation_type)(tree_type&, const order_type&,
const std::array<Key, capacity>&, const std::array<Key, capacity>&);
const operation_type operations[] = {
run_operation<0, tree_type, Key>, run_operation<1, tree_type, Key>,
run_operation<2, tree_type, Key>, run_operation<3, tree_type, Key>,
run_operation<4, tree_type, Key>, run_operation<5, tree_type, Key>
};
for (int operation = 0; operation < 6; ++operation) {
std::array<double, 7> samples;
for (std::size_t sample = 0; sample < samples.size(); ++sample) {
double elapsed = 0;
for (int round = 0; round < rounds; ++round) {
tree_type tree;
if (operation != 0) {
for (int i : order) tree.insert(at(keys, i), i);
}
const auto start = std::chrono::steady_clock::now();
std::uint64_t checksum = operations[operation](tree, order, keys, misses);
const auto stop = std::chrono::steady_clock::now();
elapsed += std::chrono::duration<double, std::nano>(stop - start).count();
// Observe the resulting values too, so update stores cannot be removed.
for (auto it = tree.begin(); it != tree.end(); ++it) checksum += static_cast<unsigned>(*it);
sink += checksum;
}
samples[sample] = elapsed / (rounds * capacity);
}
std::sort(samples.begin(), samples.end());
std::cout << (std::is_same<Key, int>::value ? "int" : "string") << ','
<< container << ',' << (shuffled ? "shuffled" : "ascending") << ','
<< names[operation] << ',' << sizeof(tree_type) << ','
<< std::fixed << std::setprecision(2) << samples[samples.size() / 2] << '\n';
}
}
}
int main(int argc, char** argv)
{
const int rounds = argc > 1 ? std::max(1, std::atoi(argv[1])) : 64;
std::cout << "key,container,order,operation,container_bytes,median_ns_per_op\n";
// avl_array2 is measured twice: completely filled by the 512 keys (every slot is used, the worst
// case for insert and erase) and with spare slots (1023 slots for the 512 keys).
for (bool shuffled : { false, true }) {
measure<int, avl_array<int, int, std::uint16_t, capacity, true> >("avl_array_fast", shuffled, rounds);
measure<int, avl_array<int, int, std::uint16_t, capacity, false> >("avl_array_small", shuffled, rounds);
measure<int, avl_array2<int, int, std::uint16_t, capacity> >("avl_array2_full", shuffled, rounds);
measure<int, avl_array2<int, int, std::uint16_t, 2 * capacity - 1> >("avl_array2_spare", shuffled, rounds);
measure<std::string, avl_array<std::string, int, std::uint16_t, capacity, true> >("avl_array_fast", shuffled, rounds);
measure<std::string, avl_array<std::string, int, std::uint16_t, capacity, false> >("avl_array_small", shuffled, rounds);
measure<std::string, avl_array2<std::string, int, std::uint16_t, capacity> >("avl_array2_full", shuffled, rounds);
measure<std::string, avl_array2<std::string, int, std::uint16_t, 2 * capacity - 1> >("avl_array2_spare", shuffled, rounds);
}
}
#endif