Skip to content

Commit a2f7b3c

Browse files
committed
Add benchmarks.
1 parent 0f93eba commit a2f7b3c

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#include <unordered_set>
2+
#include <string>
3+
#include <random>
4+
#include <vector>
5+
6+
#include "../../GenerateInput.h"
7+
#include "benchmark/benchmark.h"
8+
9+
// Generate random strings of at least 32 chars
10+
struct LongStringGenerator {
11+
static std::vector<std::string> cached_strings;
12+
13+
static void ensure_strings(size_t count) {
14+
cached_strings.clear();
15+
16+
std::mt19937_64 gen(42); // Fixed seed for reproducibility
17+
std::uniform_int_distribution<size_t> len_dist(32, 128);
18+
19+
cached_strings.reserve(count);
20+
for (size_t i = 0; i < count; i++) {
21+
std::string str(len_dist(gen), 0);
22+
for (char& c : str) {
23+
c = 'a' + (gen() % 26);
24+
}
25+
cached_strings.push_back(std::move(str));
26+
}
27+
}
28+
29+
const std::string& generate(size_t i) { return cached_strings[i]; }
30+
};
31+
32+
std::vector<std::string> LongStringGenerator::cached_strings;
33+
[[maybe_unused]] auto dummy = [] { // Pre-generate 32K strings
34+
LongStringGenerator::ensure_strings(1 << 15);
35+
return 0;
36+
}();
37+
38+
template <class Gen>
39+
static void BM_UnorderedSet_Find_EmptySet(benchmark::State& state, Gen g) {
40+
const size_t lookup_count = state.range(0);
41+
std::unordered_set<std::string> s; // Empty set
42+
43+
for (auto _ : state) {
44+
for (size_t i = 0; i < lookup_count; i++) {
45+
benchmark::DoNotOptimize(s.find(g.generate(i)));
46+
}
47+
}
48+
}
49+
50+
template <class Gen>
51+
static void BM_UnorderedSet_Find(benchmark::State& state, Gen g) {
52+
const size_t lookup_count = state.range(0);
53+
std::unordered_set<std::string> s{"hello"}; // Non-empty set
54+
55+
for (auto _ : state) {
56+
for (size_t i = 0; i < lookup_count; i++) {
57+
benchmark::DoNotOptimize(s.find(g.generate(i)));
58+
}
59+
}
60+
}
61+
62+
BENCHMARK_CAPTURE(BM_UnorderedSet_Find_EmptySet, long_string, LongStringGenerator())
63+
->RangeMultiplier(2)
64+
->Range(1 << 10, 1 << 15); // Test from 1K to 32K lookups
65+
66+
BENCHMARK_CAPTURE(BM_UnorderedSet_Find, long_string, LongStringGenerator())
67+
->RangeMultiplier(2)
68+
->Range(1 << 10, 1 << 15); // Test from 1K to 32K lookups
69+
70+
BENCHMARK_MAIN();

0 commit comments

Comments
 (0)