-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathspeed_test.cpp
361 lines (342 loc) · 13.4 KB
/
speed_test.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
358
359
360
361
#define _SCL_SECURE_NO_WARNINGS
#include "bpptree_set.h"
#include "chash_set.h"
#include "sbtree_set.h"
#include <unordered_set>
#include <set>
#include <chrono>
#include <iostream>
#include <random>
#include <cstring>
#include <string>
#include <functional>
template<size_t key_size>
struct key
{
union
{
int32_t value;
char data[key_size];
};
key() = default;
key(key const &) = default;
key(int32_t v) : value(v)
{
}
operator int32_t() const
{
return value;
}
key &operator = (key const &) = default;
bool operator < (key const &f) const
{
return value < f.value;
}
};
template<size_t N> struct std::hash<key<N>>
{
size_t operator()(key<N> const &key) const
{
return std::hash<int32_t>()(key.value);
}
};
std::vector<size_t> make_test(size_t size, size_t split)
{
std::vector<size_t> ret;
for(size_t i = 5; i < 5 + split; ++i)
{
ret.push_back(size_t(std::pow(size, 1.0 / (5 + split) * (i + 1))));
}
return ret;
}
template<class T, class C, size_t N>
struct test_core
{
test_core(std::string const &_info, T const *_data[], size_t _length, std::function<void(C &, T const *, size_t)> _prepare, std::function<size_t(C &, T const *, size_t)> _run)
: info(_info), data(_data), length(_length), prepare(_prepare), run(_run)
{
}
std::string info;
T const **data;
size_t length;
std::function<void(C &, T const *, size_t)> prepare;
std::function<size_t(C &, T const *, size_t)> run;
struct time_point_t
{
std::chrono::high_resolution_clock::time_point begin;
std::chrono::high_resolution_clock::time_point end;
};
std::vector<time_point_t> time[N];
void exec(size_t split)
{
auto count = make_test(length, split);
for(size_t i = 0; i < N; ++i)
{
for(size_t max : count)
{
time_point_t t;
size_t end = std::min(max, length);
C c;
prepare(c, data[i], end);
t.begin = std::chrono::high_resolution_clock::now();
run(c, data[i], end);
t.end = std::chrono::high_resolution_clock::now();
time[i].push_back(t);
}
}
}
};
template<class T, class C, size_t N>
class test_all
{
public:
test_all(std::string const &_info, T const *_data[N], size_t _length)
: info(_info), data(_data), length(_length)
{
test.emplace_back("insert_o", data, length,
[](C &c, T const *, size_t e)
{
},
[](C &c, T const *d, size_t e)
{
for(size_t i = 0; i < e; ++i)
{
c.emplace(int32_t(i));
}
return 0;
});
test.emplace_back("insert_r", data, length,
[](C &c, T const *, size_t e)
{
},
[](C &c, T const *d, size_t e)
{
for(size_t i = 0; i < e; ++i)
{
c.emplace(d[i]);
}
return 0;
});
test.emplace_back("foreach", data, length,
[](C &c, T const *d, size_t e)
{
for(size_t i = 0; i < e; ++i)
{
c.emplace(d[i]);
}
},
[](C &c, T const *d, size_t e)
{
size_t sum = 0;
for(auto &item : c)
{
sum += item;
}
return sum;
});
test.emplace_back("find", data, length,
[](C &c, T const *d, size_t e)
{
for(size_t i = 0; i < e; ++i)
{
c.emplace(d[i]);
}
},
[](C &c, T const *d, size_t e)
{
size_t sum = 0;
for(size_t i = 0; i < e; ++i)
{
sum += *c.find(d[i]);
}
return sum;
});
test.emplace_back("erase", data, length,
[](C &c, T const *d, size_t e)
{
for(size_t i = 0; i < e; ++i)
{
c.emplace(d[i]);
}
},
[](C &c, T const *d, size_t e)
{
for(size_t i = 0; i < e; ++i)
{
c.erase(d[i]);
}
return 0;
});
foo = [](int *ptr)
{
delete[] ptr;
};
}
void exec(size_t split)
{
for(auto &item : test)
{
foo(new int[1024]);
std::cout << info << ", ";
std::cout << item.info << ", ";
item.exec(split);
auto count = make_test(length, split);
for(size_t j = 0; j < count.size(); ++j)
{
std::vector<std::chrono::high_resolution_clock::duration> value;
double s = 0, v = 0;
for(size_t i = 0; i < N; ++i)
{
value.push_back(item.time[i][j].end - item.time[i][j].begin);
s += value.back().count();
}
s /= N;
for(size_t i = 0; i < N; ++i)
{
v += std::pow(s - value[i].count(), 2);
}
v = std::sqrt(v);
std::sort(value.begin(), value.end(), [&](std::chrono::high_resolution_clock::duration const left, std::chrono::high_resolution_clock::duration const &right)
{
return std::abs(left.count() - s) < std::abs(right.count() - s);
});
if(s < v)
{
value.pop_back();
}
std::chrono::high_resolution_clock::duration d = std::chrono::high_resolution_clock::duration();
for(auto v : value)
{
d += v;
}
std::cout << std::chrono::duration_cast<std::chrono::duration<float, std::nano>>(d).count() / value.size() / count[j] << ", ";
}
std::cout << std::endl;
}
}
std::vector<test_core<T, C, N>> test;
std::function<void(int *)> foo;
private:
std::string info;
T const **data;
size_t length;
};
int main()
{
typedef std::set<key<4 >> std_set_4 ;
typedef std::set<key<32 >> std_set_32 ;
typedef std::set<key<64 >> std_set_64 ;
typedef std::set<key<128>> std_set_128 ;
typedef std::set<key<200>> std_set_200 ;
typedef std::unordered_set<key<4 >> std_hash_4 ;
typedef std::unordered_set<key<32 >> std_hash_32 ;
typedef std::unordered_set<key<64 >> std_hash_64 ;
typedef std::unordered_set<key<128>> std_hash_128 ;
typedef std::unordered_set<key<200>> std_hash_200 ;
typedef chash_set<key<4 >> chash_set_4 ;
typedef chash_set<key<32 >> chash_set_32 ;
typedef chash_set<key<64 >> chash_set_64 ;
typedef chash_set<key<128>> chash_set_128 ;
typedef chash_set<key<200>> chash_set_200 ;
typedef bpptree_set<key<4 >> bpptree_set_4 ;
typedef bpptree_set<key<32 >> bpptree_set_32 ;
typedef bpptree_set<key<64 >> bpptree_set_64 ;
typedef bpptree_set<key<128>> bpptree_set_128;
typedef bpptree_set<key<200>> bpptree_set_200;
typedef std::multiset<key<4 >> std_mset_4 ;
typedef std::multiset<key<32 >> std_mset_32 ;
typedef std::multiset<key<64 >> std_mset_64 ;
typedef std::multiset<key<128>> std_mset_128 ;
typedef std::multiset<key<200>> std_mset_200 ;
typedef std::unordered_multiset<key<4 >> std_mhash_4 ;
typedef std::unordered_multiset<key<32 >> std_mhash_32 ;
typedef std::unordered_multiset<key<64 >> std_mhash_64 ;
typedef std::unordered_multiset<key<128>> std_mhash_128 ;
typedef std::unordered_multiset<key<200>> std_mhash_200 ;
typedef chash_multiset<key<4 >> chash_mset_4 ;
typedef chash_multiset<key<32 >> chash_mset_32 ;
typedef chash_multiset<key<64 >> chash_mset_64 ;
typedef chash_multiset<key<128>> chash_mset_128 ;
typedef chash_multiset<key<200>> chash_mset_200 ;
typedef sbtree_multiset<key<4 >> sbtree_mset_4 ;
typedef sbtree_multiset<key<32 >> sbtree_mset_32 ;
typedef sbtree_multiset<key<64 >> sbtree_mset_64 ;
typedef sbtree_multiset<key<128>> sbtree_mset_128 ;
typedef sbtree_multiset<key<200>> sbtree_mset_200 ;
typedef bpptree_multiset<key<4 >> bpptree_mset_4 ;
typedef bpptree_multiset<key<32 >> bpptree_mset_32 ;
typedef bpptree_multiset<key<64 >> bpptree_mset_64 ;
typedef bpptree_multiset<key<128>> bpptree_mset_128;
typedef bpptree_multiset<key<200>> bpptree_mset_200;
std::mt19937 mt(0);
auto mtr = std::uniform_int_distribution<int>(-200000000, 200000000);
static constexpr size_t count = 409600;
static constexpr size_t times = 5;
static constexpr size_t split = 17;
std::vector<int> v_arr[times];
for(auto &v : v_arr)
{
v.resize(count);
for(auto &value : v)
{
value = mtr(mt);
}
}
int const *v[times];
for(size_t i = 0; i < times; ++i)
{
v[i] = v_arr[i].data();
}
std::cout << "container , method, ";
for(auto item : make_test(count, split))
{
std::cout << item << ", ";
}
std::cout << std::endl;
test_all<int, std_set_4 , times>("std_set_4 ", v, count).exec(split);
test_all<int, std_hash_4 , times>("std_hash_4 ", v, count).exec(split);
test_all<int, chash_set_4 , times>("chash_set_4 ", v, count).exec(split);
test_all<int, bpptree_set_4 , times>("bpptree_set_4 ", v, count).exec(split);
test_all<int, std_set_32 , times>("std_set_32 ", v, count).exec(split);
test_all<int, std_hash_32 , times>("std_hash_32 ", v, count).exec(split);
test_all<int, chash_set_32 , times>("chash_set_32 ", v, count).exec(split);
test_all<int, bpptree_set_32 , times>("bpptree_set_32 ", v, count).exec(split);
test_all<int, std_set_64 , times>("std_set_64 ", v, count).exec(split);
test_all<int, std_hash_64 , times>("std_hash_64 ", v, count).exec(split);
test_all<int, chash_set_64 , times>("chash_set_64 ", v, count).exec(split);
test_all<int, bpptree_set_64 , times>("bpptree_set_64 ", v, count).exec(split);
test_all<int, std_set_128 , times>("std_set_128 ", v, count).exec(split);
test_all<int, std_hash_128 , times>("std_hash_128 ", v, count).exec(split);
test_all<int, chash_set_128 , times>("chash_set_128 ", v, count).exec(split);
test_all<int, bpptree_set_128, times>("bpptree_set_128", v, count).exec(split);
test_all<int, std_set_200 , times>("std_set_200 ", v, count).exec(split);
test_all<int, std_hash_200 , times>("std_hash_200 ", v, count).exec(split);
test_all<int, chash_set_200 , times>("chash_set_200 ", v, count).exec(split);
test_all<int, bpptree_set_200, times>("bpptree_set_200", v, count).exec(split);
test_all<int, std_mset_4 , times>("std_mset_4 ", v, count).exec(split);
test_all<int, std_mhash_4 , times>("std_mhash_4 ", v, count).exec(split);
test_all<int, chash_mset_4 , times>("chash_mset_4 ", v, count).exec(split);
test_all<int, sbtree_mset_4 , times>("sbtree_mset_4 ", v, count).exec(split);
test_all<int, bpptree_mset_4 , times>("bpptree_mset_4 ", v, count).exec(split);
test_all<int, std_mset_32 , times>("std_mset_32 ", v, count).exec(split);
test_all<int, std_mhash_32 , times>("std_mhash_32 ", v, count).exec(split);
test_all<int, chash_mset_32 , times>("chash_mset_32 ", v, count).exec(split);
test_all<int, sbtree_mset_32 , times>("sbtree_mset_32 ", v, count).exec(split);
test_all<int, bpptree_mset_32 , times>("bpptree_mset_32 ", v, count).exec(split);
test_all<int, std_mset_64 , times>("std_mset_64 ", v, count).exec(split);
test_all<int, std_mhash_64 , times>("std_mhash_64 ", v, count).exec(split);
test_all<int, chash_mset_64 , times>("chash_mset_64 ", v, count).exec(split);
test_all<int, sbtree_mset_64 , times>("sbtree_mset_64 ", v, count).exec(split);
test_all<int, bpptree_mset_64 , times>("bpptree_mset_64 ", v, count).exec(split);
test_all<int, std_mset_128 , times>("std_mset_128 ", v, count).exec(split);
test_all<int, std_mhash_128 , times>("std_mhash_128 ", v, count).exec(split);
test_all<int, chash_mset_128 , times>("chash_mset_128 ", v, count).exec(split);
test_all<int, sbtree_mset_128 , times>("sbtree_mset_128 ", v, count).exec(split);
test_all<int, bpptree_mset_128, times>("bpptree_mset_128", v, count).exec(split);
test_all<int, std_mset_200 , times>("std_mset_200 ", v, count).exec(split);
test_all<int, std_mhash_200 , times>("std_mhash_200 ", v, count).exec(split);
test_all<int, chash_mset_200 , times>("chash_mset_200 ", v, count).exec(split);
test_all<int, sbtree_mset_200 , times>("sbtree_mset_200 ", v, count).exec(split);
test_all<int, bpptree_mset_200, times>("bpptree_mset_200", v, count).exec(split);
}