-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPerformanceTest.cpp
More file actions
82 lines (66 loc) · 2.66 KB
/
Copy pathPerformanceTest.cpp
File metadata and controls
82 lines (66 loc) · 2.66 KB
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
#include <iostream>
#include <vector>
#include <chrono>
#include <unordered_map>
#include <numeric>
#include <algorithm>
#include <random>
extern "C" {
#define HM_HASHMAP_IMPLEMENTATION
#include "HashMap.h"
}
// Define the type helper for the benchmark
HM_DEFINE_TYPE(U64, uint64_t)
using namespace std::chrono;
void run_benchmark(uint32_t num_elements) {
std::vector<uint64_t> keys(num_elements);
std::iota(keys.begin(), keys.end(), 1000); // Sequential keys
// Shuffle keys to test real-world hashing performance
std::random_device rd;
std::mt19937 g(rd());
std::shuffle(keys.begin(), keys.end(), g);
std::cout << "Benchmarking with " << num_elements << " elements...\n\n";
// --- std::unordered_map ---
{
std::unordered_map<uint64_t, uint64_t> std_map;
std_map.reserve(num_elements);
auto start = high_resolution_clock::now();
for (auto k : keys) std_map[k] = k;
auto end = high_resolution_clock::now();
std::cout << "std::unordered_map Insert: " << duration_cast<milliseconds>(end - start).count() << "ms\n";
start = high_resolution_clock::now();
volatile uint64_t sum = 0;
for (auto k : keys) sum += std_map[k];
end = high_resolution_clock::now();
std::cout << "std::unordered_map Lookup: " << duration_cast<milliseconds>(end - start).count() << "ms\n";
start = high_resolution_clock::now();
for (auto k : keys) std_map.erase(k);
end = high_resolution_clock::now();
std::cout << "std::unordered_map Erase: " << duration_cast<milliseconds>(end - start).count() << "ms\n\n";
}
// --- HashMap ---
{
HashMap my_map = HMCreate(num_elements, sizeof(uint64_t));
auto start = high_resolution_clock::now();
for (auto k : keys) HMInsertU64(&my_map, k, k);
auto end = high_resolution_clock::now();
std::cout << "HashMap Insert: " << duration_cast<milliseconds>(end - start).count() << "ms\n";
start = high_resolution_clock::now();
volatile uint64_t sum = 0;
for (auto k : keys) {
uint64_t val;
if (HMTryGetU64(&my_map, k, &val)) sum += val;
}
end = high_resolution_clock::now();
std::cout << "HashMap Lookup: " << duration_cast<milliseconds>(end - start).count() << "ms\n";
start = high_resolution_clock::now();
for (auto k : keys) HMErase(&my_map, k);
end = high_resolution_clock::now();
std::cout << "HashMap Erase: " << duration_cast<milliseconds>(end - start).count() << "ms\n";
HMDestroy(&my_map);
}
}
int main() {
run_benchmark(1'000'000);
return 0;
}