-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash.cpp
251 lines (221 loc) · 6.97 KB
/
hash.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
// Copyright 2020 The Division Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Author [email protected] && [email protected]
#include "butil/containers/flat_map.h"
#include "flat_hash_map/flat_hash_map.hpp"
#include "internal/city.h"
#include "internal/murmurhash3.h"
#include "internal/wyhash.h"
#include "internal/xxhash.h"
#include "parallel_hashmap/phmap.h"
#include "tsl/ordered_map.h"
#include "tsl/robin_map.h"
#include "tsl/sparse_map.h"
#include <absl/container/flat_hash_map.h>
#include <absl/hash/hash.h>
#include <benchmark/benchmark.h>
#include <fstream>
#include <string>
#include <vector>
constexpr int N = 1024;
static unsigned long xorshf96()
{ /* A George Marsaglia generator, period 2^96-1 */
static unsigned long x = 123456789, y = 362436069, z = 521288629;
unsigned long t;
x ^= x << 16;
x ^= x >> 5;
x ^= x << 1;
t = x;
x = y;
y = z;
z = t ^ x ^ y;
return z;
}
static inline unsigned long random_()
{
return xorshf96();
}
std::vector<std::string> vec;
void init(std::vector<std::string> &v1)
{
v1.resize(N);
for (size_t i = 0; i < N; i++)
{
v1[i] = std::to_string(random_()) + std::to_string(random_()) + std::to_string(random_());
}
}
static void BM_StdHash(benchmark::State &state)
{
for (auto _ : state)
{
auto idx = random_() % vec.size();
auto r = std::_Hash_impl::hash(vec[idx]);
benchmark::DoNotOptimize(r);
}
}
BENCHMARK(BM_StdHash);
static void BM_StdHashFnv(benchmark::State &state)
{
for (auto _ : state)
{
auto idx = random_() % vec.size();
auto r = std::_Fnv_hash_impl::hash(vec[idx]);
benchmark::DoNotOptimize(r);
}
}
BENCHMARK(BM_StdHashFnv);
static void BM_CityHash(benchmark::State &state)
{
for (auto _ : state)
{
auto idx = random_() % vec.size();
auto r = CityHash64(vec[idx].c_str(), vec[idx].size());
benchmark::DoNotOptimize(r);
}
}
BENCHMARK(BM_CityHash);
static void BM_Murmur(benchmark::State &state)
{
for (auto _ : state)
{
auto idx = random_() % vec.size();
auto out = 0;
butil::MurmurHash3_x86_32(vec[idx].c_str(), vec[idx].size(), 0, &out);
benchmark::DoNotOptimize(out);
}
}
BENCHMARK(BM_Murmur);
static void BM_WyHash(benchmark::State &state)
{
for (auto _ : state)
{
auto idx = random_() % vec.size();
auto r = wyhash(vec[idx].c_str(), vec[idx].size(), 0, _wyp);
benchmark::DoNotOptimize(r);
}
}
BENCHMARK(BM_WyHash);
static void BM_XXHash(benchmark::State &state)
{
for (auto _ : state)
{
auto idx = random_() % vec.size();
auto r = XXH64(vec[idx].c_str(), vec[idx].size(), 0);
benchmark::DoNotOptimize(r);
}
}
BENCHMARK(BM_XXHash);
static void BM_AbseilHash(benchmark::State &state)
{
for (auto _ : state)
{
auto idx = random_() % vec.size();
auto r = absl::Hash<std::string>{}(vec[idx]);
benchmark::DoNotOptimize(r);
}
}
BENCHMARK(BM_AbseilHash);
static void BM_DefaultHash(benchmark::State &state)
{
for (auto _ : state)
{
auto idx = random_() % vec.size();
auto r = butil::DefaultHasher<std::string>{}(vec[idx]);
benchmark::DoNotOptimize(r);
}
}
BENCHMARK(BM_DefaultHash);
struct Hasher
{
size_t operator()(const std::string &str) const
{
// return butil::DefaultHasher<std::string>{}(str);
return wyhash(str.c_str(), str.size(), 0, _wyp);
// return XXH64(str.c_str(), str.size(), 0);
return std::hash<std::string>{}(str);
// size_t out = 0;
// butil::MurmurHash3_x86_32(str.c_str(), str.size(), 0, &out);
// return out;
}
};
std::vector<std::string> keys;
template <class M> static void BenchUnOrderMapString(benchmark::State &state)
{
M m;
m.reserve(keys.size());
for (size_t i = 0; i < keys.size(); i++)
{
m[keys[i]] = i;
}
for (auto _ : state)
{
auto kIndex = random_() % keys.size();
auto c = m.find(keys[kIndex]);
benchmark::DoNotOptimize(c);
}
}
BENCHMARK_TEMPLATE(BenchUnOrderMapString, std::unordered_map<std::string, int>);
BENCHMARK_TEMPLATE(BenchUnOrderMapString, std::unordered_map<std::string, int, Hasher>);
BENCHMARK_TEMPLATE(BenchUnOrderMapString, ska::flat_hash_map<std::string, int>);
BENCHMARK_TEMPLATE(BenchUnOrderMapString, ska::flat_hash_map<std::string, int, Hasher>);
BENCHMARK_TEMPLATE(BenchUnOrderMapString, phmap::flat_hash_map<std::string, int>);
BENCHMARK_TEMPLATE(BenchUnOrderMapString, phmap::flat_hash_map<std::string, int, Hasher>);
BENCHMARK_TEMPLATE(BenchUnOrderMapString, absl::flat_hash_map<std::string, int>);
BENCHMARK_TEMPLATE(BenchUnOrderMapString, absl::flat_hash_map<std::string, int, absl::Hash<std::string>>);
BENCHMARK_TEMPLATE(BenchUnOrderMapString, absl::flat_hash_map<std::string, int, Hasher>);
BENCHMARK_TEMPLATE(BenchUnOrderMapString, tsl::robin_map<std::string, int>);
BENCHMARK_TEMPLATE(BenchUnOrderMapString, tsl::robin_map<std::string, int, Hasher>);
BENCHMARK_TEMPLATE(BenchUnOrderMapString, tsl::vector_map<std::string, int>);
BENCHMARK_TEMPLATE(BenchUnOrderMapString, tsl::vector_map<std::string, int, Hasher>);
BENCHMARK_TEMPLATE(BenchUnOrderMapString, tsl::sparse_map<std::string, int>);
BENCHMARK_TEMPLATE(BenchUnOrderMapString, tsl::sparse_map<std::string, int, Hasher>);
template <class M> static void BenchFlatMapString(benchmark::State &state)
{
M m;
m.init(keys.size());
for (size_t i = 0; i < keys.size(); i++)
{
m[keys[i]] = i;
}
for (auto _ : state)
{
auto kIndex = random_() % keys.size();
auto c = m.seek(keys[kIndex]);
benchmark::DoNotOptimize(c);
}
}
BENCHMARK_TEMPLATE(BenchFlatMapString, butil::FlatMap<std::string, int>);
BENCHMARK_TEMPLATE(BenchFlatMapString, butil::FlatMap<std::string, int, std::hash<std::string>>);
BENCHMARK_TEMPLATE(BenchFlatMapString, butil::FlatMap<std::string, int, Hasher>);
int main(int argc, char **argv)
{
// init(vec);
keys.resize(1024);
for (size_t i = 0; i < 1024; i++)
{
keys[i] = std::to_string(random_()) + std::to_string(random_());
}
std::fstream fsKeys("keys.txt", std::ios::in);
// std::stringstream ss;
// ss<<fsKeys.rdbuf();
std::string strKey;
while (fsKeys >> strKey)
{
vec.emplace_back(std::move(strKey));
}
// keys = vec;
benchmark::Initialize(&argc, argv);
benchmark::RunSpecifiedBenchmarks();
return 0;
}