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
|
// https://www.codeproject.com/Tips/5255442/Cplusplus14-20-Heterogeneous-Lookup-Benchmark
// https://github.com/shaovoon/cpp_hetero_lookup_bench
#include <iostream>
#include <iomanip>
#include <chrono>
#define i_static
#include <stc/cstr.h> // string
#define i_static
#include <stc/csview.h> // string_view
#define i_key_str
#include <stc/cvec.h> // vec of cstr with const char* lookup
#define i_type cvec_sv // override default type name (cvec_csview)
#define i_key csview
#define i_cmp csview_cmp
#include <stc/cvec.h> // cvec_vs: vec of csview
#define i_key_str
#define i_val size_t
#include <stc/csmap.h> // sorted map of cstr, const char* lookup
#define i_key_ssv
#define i_val size_t
#include <stc/csmap.h> // sorted map of cstr, csview lookup
#define i_key_str
#define i_val size_t
#include <stc/cmap.h> // unordered map of cstr, const char* lookup
#define i_key_ssv
#define i_val size_t
#include <stc/cmap.h> // unordered map of cstr, csview lookup
cvec_str read_file(const char* name)
{
cvec_str data = cvec_str_init();
c_auto (cstr, line)
c_with (FILE* f = fopen(name, "r"), fclose(f))
while (cstr_getline(&line, f))
cvec_str_emplace_back(&data, cstr_str(&line));
return data;
}
class timer
{
public:
timer() = default;
void start(const std::string& text_)
{
text = text_;
begin = std::chrono::high_resolution_clock::now();
}
void stop()
{
auto end = std::chrono::high_resolution_clock::now();
auto dur = end - begin;
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(dur).count();
std::cout << std::setw(32) << text << " timing:" << std::setw(5) << ms << "ms" << std::endl;
}
private:
std::string text;
std::chrono::high_resolution_clock::time_point begin;
};
void initShortStringVec(cvec_str* vs, cvec_sv* vsv)
{
cvec_str_drop(vs);
cvec_sv_clear(vsv);
*vs = read_file("names.txt");
/*
cvec_str_emplace_back(vs, "Susan");
cvec_str_emplace_back(vs, "Jason");
cvec_str_emplace_back(vs, "Lily");
cvec_str_emplace_back(vs, "Michael");
cvec_str_emplace_back(vs, "Mary");
cvec_str_emplace_back(vs, "Jerry");
cvec_str_emplace_back(vs, "Jenny");
cvec_str_emplace_back(vs, "Klaus");
cvec_str_emplace_back(vs, "Celine");
cvec_str_emplace_back(vs, "Kenny");
cvec_str_emplace_back(vs, "Kelly");
cvec_str_emplace_back(vs, "Jackson");
cvec_str_emplace_back(vs, "Mandy");
cvec_str_emplace_back(vs, "Terry");
cvec_str_emplace_back(vs, "Sandy");
cvec_str_emplace_back(vs, "Billy");
cvec_str_emplace_back(vs, "Cindy");
cvec_str_emplace_back(vs, "Phil");
cvec_str_emplace_back(vs, "Lindy");
cvec_str_emplace_back(vs, "David");
*/
size_t num = 0;
c_foreach (i, cvec_str, *vs)
{
cvec_sv_push_back(vsv, cstr_sv(i.ref));
num += cstr_size(i.ref);
}
std::cout << "num strings: " << cvec_sv_size(vsv) << std::endl;
std::cout << "avg str len: " << num / (float)cvec_sv_size(vsv) << std::endl;
}
void initLongStringVec(cvec_str* vs, cvec_sv* vsv)
{
cvec_str_drop(vs);
cvec_sv_clear(vsv);
*vs = read_file("names.txt");
c_foreach (i, cvec_str, *vs) {
cstr_append_s(i.ref, *i.ref);
cstr_append_s(i.ref, *i.ref);
cstr_append_s(i.ref, *i.ref);
}
/*
cvec_str_emplace_back(vs, "Susan Susan Susan Susan Susan Susan");
cvec_str_emplace_back(vs, "Jason Jason Jason Jason Jason Jason");
cvec_str_emplace_back(vs, "Lily Lily Lily Lily Lily Lily");
cvec_str_emplace_back(vs, "Michael Michael Michael Michael Michael Michael");
cvec_str_emplace_back(vs, "Mary Mary Mary Mary Mary Mary");
cvec_str_emplace_back(vs, "Jerry Jerry Jerry Jerry Jerry Jerry");
cvec_str_emplace_back(vs, "Jenny Jenny Jenny Jenny Jenny Jenny");
cvec_str_emplace_back(vs, "Klaus Klaus Klaus Klaus Klaus Klaus");
cvec_str_emplace_back(vs, "Celine Celine Celine Celine Celine Celine");
cvec_str_emplace_back(vs, "Kenny Kenny Kenny Kenny Kenny Kenny");
cvec_str_emplace_back(vs, "Kelly Kelly Kelly Kelly Kelly Kelly");
cvec_str_emplace_back(vs, "Jackson Jackson Jackson Jackson Jackson Jackson");
cvec_str_emplace_back(vs, "Mandy Mandy Mandy Mandy Mandy Mandy");
cvec_str_emplace_back(vs, "Terry Terry Terry Terry Terry Terry");
cvec_str_emplace_back(vs, "Sandy Sandy Sandy Sandy Sandy Sandy");
cvec_str_emplace_back(vs, "Billy Billy Billy Billy Billy Billy");
cvec_str_emplace_back(vs, "Cindy Cindy Cindy Cindy Cindy Cindy");
cvec_str_emplace_back(vs, "Phil Phil Phil Phil Phil Phil");
cvec_str_emplace_back(vs, "Lindy Lindy Lindy Lindy Lindy Lindy");
cvec_str_emplace_back(vs, "David David David David David David");
*/
size_t num = 0;
c_foreach (i, cvec_str, *vs)
{
cvec_sv_push_back(vsv, cstr_sv(i.ref));
num += cstr_size(i.ref);
}
std::cout << "num strings: " << cvec_sv_size(vsv) << std::endl;
std::cout << "avg str len: " << num / (float)cvec_sv_size(vsv) << std::endl;
}
void initMaps(const cvec_str* vs, csmap_str* mapTrans, csmap_ssv* mapSview,
cmap_str* unordmapTrans, cmap_ssv* unordmapSview)
{
csmap_str_clear(mapTrans);
csmap_ssv_clear(mapSview);
cmap_str_clear(unordmapTrans);
cmap_ssv_clear(unordmapSview);
size_t n = 0;
c_foreach (i, cvec_str, *vs)
{
csmap_str_insert(mapTrans, cstr_clone(*i.ref), n);
csmap_ssv_insert(mapSview, cstr_clone(*i.ref), n);
cmap_str_insert(unordmapTrans, cstr_clone(*i.ref), n);
cmap_ssv_insert(unordmapSview, cstr_clone(*i.ref), n);
++n;
}
}
void benchmark(
const cvec_str* vec_string,
const cvec_sv* vec_stringview,
const csmap_str* mapTrans,
const csmap_ssv* mapSview,
const cmap_str* unordmapTrans,
const cmap_ssv* unordmapSview);
//const size_t MAX_LOOP = 1000000;
const size_t MAX_LOOP = 2000;
int main()
{
c_auto (cvec_str, vec_string)
c_auto (cvec_sv, vec_stringview)
c_auto (csmap_str, mapTrans)
c_auto (csmap_ssv, mapSview)
c_auto (cmap_str, unordmapTrans)
c_auto (cmap_ssv, unordmapSview)
{
std::cout << "Short String Benchmark" << std::endl;
std::cout << "======================" << std::endl;
initShortStringVec(&vec_string, &vec_stringview);
initMaps(&vec_string, &mapTrans, &mapSview,
&unordmapTrans, &unordmapSview);
for (int i=0; i<3; ++i)
benchmark(
&vec_string,
&vec_stringview,
&mapTrans,
&mapSview,
&unordmapTrans,
&unordmapSview);
std::cout << "Long String Benchmark" << std::endl;
std::cout << "=====================" << std::endl;
initLongStringVec(&vec_string, &vec_stringview);
initMaps(&vec_string, &mapTrans, &mapSview,
&unordmapTrans, &unordmapSview);
for (int i=0; i<3; ++i)
benchmark(
&vec_string,
&vec_stringview,
&mapTrans,
&mapSview,
&unordmapTrans,
&unordmapSview);
}
return 0;
}
void benchmark(
const cvec_str* vec_string,
const cvec_sv* vec_stringview,
const csmap_str* mapTrans,
const csmap_ssv* mapSview,
const cmap_str* unordmapTrans,
const cmap_ssv* unordmapSview)
{
size_t grandtotal = 0;
size_t total = 0;
timer stopwatch;
total = 0;
stopwatch.start("Trans Map with char*");
for (size_t i = 0; i < MAX_LOOP; ++i)
{
c_foreach (j, cvec_str, *vec_string)
{
const csmap_str_value* v = csmap_str_get(mapTrans, cstr_str(j.ref));
if (v)
total += v->second;
}
}
grandtotal += total;
stopwatch.stop();
total = 0;
stopwatch.start("Trans Map with string_view");
for (size_t i = 0; i < MAX_LOOP; ++i)
{
c_foreach (j, cvec_sv, *vec_stringview)
{
const csmap_ssv_value* v = csmap_ssv_get(mapSview, *j.ref);
if (v)
total += v->second;
}
}
grandtotal += total;
stopwatch.stop();
total = 0;
stopwatch.start("Trans Unord Map with char*");
for (size_t i = 0; i < MAX_LOOP; ++i)
{
c_foreach (j, cvec_str, *vec_string)
{
const cmap_str_value* v = cmap_str_get(unordmapTrans, cstr_str(j.ref));
if (v)
total += v->second;
}
}
grandtotal += total;
stopwatch.stop();
total = 0;
stopwatch.start("Trans Unord Map with string_view");
for (size_t i = 0; i < MAX_LOOP; ++i)
{
c_foreach (j, cvec_sv, *vec_stringview)
{
const cmap_ssv_value* v = cmap_ssv_get(unordmapSview, *j.ref);
if (v)
total += v->second;
}
}
grandtotal += total;
stopwatch.stop();
std::cout << "grandtotal:" << grandtotal << " <--- Ignore this\n" << std::endl;
}
|