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
|
// 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>
#include <string>
#include <vector>
#include <map>
#include <unordered_map>
#define i_val_str
#include <stc/cvec.h>
std::vector<std::string> read_file(const char* name)
{
std::vector<std::string> data;
c_auto (cstr, line)
c_autovar (FILE* f = fopen(name, "r"), fclose(f))
while (cstr_getline(&line, f))
data.emplace_back(line.str);
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(std::vector<std::string>& vs)
{
vs.clear();
vs = read_file("names.txt");
size_t num = 0;
for (size_t i = 0; i < vs.size(); ++i)
{
num += vs[i].size();
}
printf("avg len: %f\n", (float)num / vs.size());
}
void initLongStringVec(std::vector<std::string>& vs)
{
vs.clear();
vs = read_file("names.txt");
size_t num = 0;
vs[0] += vs[1];
vs[0] += vs[2];
vs[0] += vs[3];
for (size_t i = 1; i < vs.size(); ++i)
{
vs[i] += vs[i];
vs[i] += vs[i];
vs[i] += vs[0];
vs[i] += vs[i];
num += vs[i].size();
}
printf("avg len: %f\n", (float)num / vs.size());
}
void initMapNormal(const std::vector<std::string>& vs,
std::map<std::string, size_t>& mapNormal,
std::unordered_map<std::string, size_t>& unordmapNormal)
{
mapNormal.clear();
unordmapNormal.clear();
for (size_t i = 0; i < vs.size(); ++i)
{
mapNormal.insert(std::make_pair(vs.at(i), i));
unordmapNormal.insert(std::make_pair(vs.at(i), i));
}
}
/*
struct string_hash {
using is_transparent = void;
using hash_type = std::hash<std::string_view>; // just a helper local type
size_t operator()(const std::string& txt) const { return hash_type{}(txt); }
size_t operator()(const char* txt) const { return hash_type{}(txt); }
};*/
void benchmark(
const std::vector<std::string>& vec_string,
const std::map<std::string, size_t>& mapNormal,
const std::unordered_map<std::string, size_t>& unordmapNormal);
const size_t MAX_LOOP = 2000;
int main()
{
std::vector<std::string> vec_string;
std::map<std::string, size_t> mapNormal;
std::unordered_map<std::string, size_t> unordmapNormal;
initShortStringVec(vec_string);
initMapNormal(vec_string, mapNormal, unordmapNormal);
std::cout << "Short String Benchmark" << std::endl;
std::cout << "======================" << std::endl;
benchmark(vec_string, mapNormal, unordmapNormal);
std::cout << "Long String Benchmark" << std::endl;
std::cout << "=====================" << std::endl;
initLongStringVec(vec_string);
initMapNormal(vec_string, mapNormal, unordmapNormal);
benchmark(vec_string, mapNormal, unordmapNormal);
return 0;
}
void benchmark(
const std::vector<std::string>& vec_string,
const std::map<std::string, size_t>& mapNormal,
const std::unordered_map<std::string, size_t>& unordmapNormal)
{
size_t grandtotal = 0;
size_t total = 0;
timer stopwatch;
total = 0;
stopwatch.start("Normal Map with string");
for (size_t i = 0; i < MAX_LOOP; ++i)
{
for (size_t j = 0; j < vec_string.size(); ++j)
{
const auto& it = mapNormal.find(vec_string[j]);
if(it!=mapNormal.cend())
total += it->second;
}
}
grandtotal += total;
stopwatch.stop();
total = 0;
stopwatch.start("Normal Map with char*");
for (size_t i = 0; i < MAX_LOOP; ++i)
{
for (size_t j = 0; j < vec_string.size(); ++j)
{
const auto& it = mapNormal.find(vec_string[j].c_str());
if (it != mapNormal.cend())
total += it->second;
}
}
grandtotal += total;
stopwatch.stop();
total = 0;
stopwatch.start("Normal Unord Map with string");
for (size_t i = 0; i < MAX_LOOP; ++i)
{
for (size_t j = 0; j < vec_string.size(); ++j)
{
const auto& it = unordmapNormal.find(vec_string[j]);
if (it != unordmapNormal.cend())
total += it->second;
}
}
grandtotal += total;
stopwatch.stop();
total = 0;
stopwatch.start("Normal Unord Map with char*");
for (size_t i = 0; i < MAX_LOOP; ++i)
{
for (size_t j = 0; j < vec_string.size(); ++j)
{
const auto& it = unordmapNormal.find(vec_string[j].c_str());
if (it != unordmapNormal.cend())
total += it->second;
}
}
grandtotal += total;
stopwatch.stop();
std::cout << "C++ grandtotal:" << grandtotal << " <--- Ignore this\n" << std::endl;
}
|