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
|
#define i_static
#include <stc/crand.h>
#define i_static
#include <stc/cstr.h>
#include <cmath>
#include <string>
#include <unordered_map>
#include <stdexcept>
#include "../external/ankerl/unordered_dense.h"
#include "../external/skarupke/flat_hash_map.hpp"
#include "../external/tsl/robin_map.h"
#define PICOBENCH_IMPLEMENT_WITH_MAIN
#include "picobench.hpp"
enum {N1 = 4000000, S1 = 1, MaxLoadFactor100 = 80};
uint64_t seed = time(NULL);
template <class K, class V> using umap = std::unordered_map<K, V>;
template <class K, class V> using fmap = ska::flat_hash_map<K, V>;
template <class K, class V> using tmap = tsl::robin_map<K, V>;
template <class K, class V> using dmap = ankerl::unordered_dense::map<K, V>;
#define DEFMAP(map, ...) \
using u##map = umap __VA_ARGS__; \
using f##map = fmap __VA_ARGS__; \
using t##map = tmap __VA_ARGS__; \
using d##map = dmap __VA_ARGS__
DEFMAP(map_i, <int32_t, int32_t>);
DEFMAP(map_x, <uint64_t, uint64_t>);
DEFMAP(map_s, <std::string, std::string>);
#define i_key int32_t
#define i_val int32_t
#define i_tag i
#define i_max_load_factor float(MaxLoadFactor100) / 100.0f
#include <stc/cmap.h>
#define i_key uint64_t
#define i_val uint64_t
#define i_tag x
#define i_max_load_factor float(MaxLoadFactor100) / 100.0f
#include <stc/cmap.h>
#define i_key_str
#define i_val_str
#define i_max_load_factor float(MaxLoadFactor100) / 100.0f
#include <stc/cmap.h>
PICOBENCH_SUITE("Map1");
template <class MapInt>
static void ins_and_erase_i(picobench::state& s)
{
MapInt map;
map.max_load_factor((int)MaxLoadFactor100 / 100.0);
csrand(seed);
picobench::scope scope(s);
c_forrange (s.iterations())
map[crand()];
map.clear();
csrand(seed);
c_forrange (s.iterations())
map[crand()];
csrand(seed);
c_forrange (s.iterations())
map.erase(crand());
s.set_result(map.size());
}
/*
static void ins_and_erase_cmap_i(picobench::state& s)
{
cmap_i map = cmap_i_init();
csrand(seed);
picobench::scope scope(s);
c_forrange (s.iterations())
cmap_i_insert(&map, crand(), 0);
cmap_i_clear(&map);
csrand(seed);
c_forrange (s.iterations())
cmap_i_insert(&map, crand(), 0);
csrand(seed);
c_forrange (s.iterations())
cmap_i_erase(&map, crand());
s.set_result(cmap_i_size(&map));
cmap_i_drop(&map);
}
*/
static void ins_and_erase_cmap_x(picobench::state& s)
{
cmap_x map = cmap_x_init();
csrand(seed);
picobench::scope scope(s);
c_forrange (s.iterations())
cmap_x_insert(&map, crand(), 0);
cmap_x_clear(&map);
csrand(seed);
c_forrange (s.iterations())
cmap_x_insert(&map, crand(), 0);
csrand(seed);
c_forrange (s.iterations())
cmap_x_erase(&map, crand());
s.set_result(cmap_x_size(&map));
cmap_x_drop(&map);
}
#define P samples(S1).iterations({N1/4})
PICOBENCH(ins_and_erase_i<umap_x>).P;
PICOBENCH(ins_and_erase_i<dmap_x>).P;
PICOBENCH(ins_and_erase_i<fmap_x>).P;
PICOBENCH(ins_and_erase_i<tmap_x>).P;
PICOBENCH(ins_and_erase_cmap_x).P;
#undef P
PICOBENCH_SUITE("Map2");
template <class MapInt>
static void ins_and_access_i(picobench::state& s)
{
uint64_t mask = (1ull << s.arg()) - 1;
size_t result = 0;
MapInt map;
map.max_load_factor((int)MaxLoadFactor100 / 100.0);
csrand(seed);
picobench::scope scope(s);
c_forrange (N1)
result += ++map[crand() & mask];
s.set_result(result);
}
static void ins_and_access_cmap_i(picobench::state& s)
{
uint64_t mask = (1ull << s.arg()) - 1;
size_t result = 0;
cmap_i map = cmap_i_init();
csrand(seed);
picobench::scope scope(s);
c_forrange (N1)
result += ++cmap_i_insert(&map, crand() & mask, 0).ref->second;
s.set_result(result);
cmap_i_drop(&map);
}
#define P samples(S1).iterations({N1, N1, N1, N1}).args({18, 23, 25, 31})
PICOBENCH(ins_and_access_i<umap_i>).P;
PICOBENCH(ins_and_access_i<dmap_i>).P;
PICOBENCH(ins_and_access_i<fmap_i>).P;
PICOBENCH(ins_and_access_i<tmap_i>).P;
PICOBENCH(ins_and_access_cmap_i).P;
#undef P
PICOBENCH_SUITE("Map3");
static void randomize(char* str, size_t len) {
for (size_t k=0; k < len; ++k) {
union {uint64_t i; char c[8];} r = {.i = crand()};
for (unsigned i=0; i<8 && k<len; ++k, ++i)
str[k] = (r.c[i] & 63) + 48;
}
}
template <class MapStr>
static void ins_and_access_s(picobench::state& s)
{
std::string str(s.arg(), 'x');
size_t result = 0;
MapStr map;
map.max_load_factor((int)MaxLoadFactor100 / 100.0);
csrand(seed);
picobench::scope scope(s);
c_forrange (s.iterations()) {
randomize(&str[0], str.size());
map.emplace(str, str);
randomize(&str[0], str.size());
result += map.erase(str);
}
s.set_result(result + map.size());
}
static void ins_and_access_cmap_s(picobench::state& s)
{
cstr str = cstr_with_size(s.arg(), 'x');
char* buf = cstr_data(&str);
size_t result = 0;
cmap_str map = cmap_str_init();
csrand(seed);
picobench::scope scope(s);
c_forrange (s.iterations()) {
randomize(buf, s.arg());
//if (s.arg() > 30) { printf("%s\n", buf); exit(0); }
cmap_str_emplace(&map, buf, buf);
randomize(buf, s.arg());
result += cmap_str_erase(&map, buf);
}
s.set_result(result + cmap_str_size(&map));
cstr_drop(&str);
cmap_str_drop(&map);
}
#define P samples(S1).iterations({N1/5, N1/5, N1/5, N1/10, N1/40}).args({13, 7, 8, 100, 1000})
PICOBENCH(ins_and_access_s<umap_s>).P;
PICOBENCH(ins_and_access_s<dmap_s>).P;
PICOBENCH(ins_and_access_s<fmap_s>).P;
PICOBENCH(ins_and_access_s<tmap_s>).P;
PICOBENCH(ins_and_access_cmap_s).P;
#undef P
PICOBENCH_SUITE("Map4");
template <class MapX>
static void iterate_x(picobench::state& s)
{
MapX map;
map.max_load_factor((int)MaxLoadFactor100 / 100.0);
uint64_t K = (1ull << s.arg()) - 1;
picobench::scope scope(s);
csrand(seed);
size_t result = 0;
// measure insert then iterate whole map
c_forrange (n, s.iterations()) {
map[crand()] = n;
if (!(n & K)) for (auto const& keyVal : map)
result += keyVal.second;
}
// reset rng back to inital state
csrand(seed);
// measure erase then iterate whole map
c_forrange (n, s.iterations()) {
map.erase(crand());
if (!(n & K)) for (auto const& keyVal : map)
result += keyVal.second;
}
s.set_result(result);
}
static void iterate_cmap_x(picobench::state& s)
{
cmap_x map = cmap_x_init();
uint64_t K = (1ull << s.arg()) - 1;
picobench::scope scope(s);
csrand(seed);
size_t result = 0;
// measure insert then iterate whole map
c_forrange (n, s.iterations()) {
cmap_x_insert_or_assign(&map, crand(), n);
if (!(n & K)) c_foreach (i, cmap_x, map)
result += i.ref->second;
}
// reset rng back to inital state
csrand(seed);
// measure erase then iterate whole map
c_forrange (n, s.iterations()) {
cmap_x_erase(&map, crand());
if (!(n & K)) c_foreach (i, cmap_x, map)
result += i.ref->second;
}
s.set_result(result);
cmap_x_drop(&map);
}
#define P samples(S1).iterations({N1/20}).args({12})
PICOBENCH(iterate_x<umap_x>).P;
PICOBENCH(iterate_x<dmap_x>).P;
PICOBENCH(iterate_x<fmap_x>).P;
PICOBENCH(iterate_x<tmap_x>).P;
PICOBENCH(iterate_cmap_x).P;
#undef P
|