-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathint_map_performance.cpp
More file actions
50 lines (37 loc) · 1.01 KB
/
int_map_performance.cpp
File metadata and controls
50 lines (37 loc) · 1.01 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
#include <chrono>
#include <cstdint>
#include <format>
#include <iostream>
#include <map>
#include <random>
#include <unordered_map>
auto getNow() {
return std::chrono::system_clock::now();
}
int main() {
constexpr static uint_fast64_t SEED = 64;
constexpr static uint64_t COUNT = 10'000'000;
std::map<uint64_t, char> map;
std::mt19937_64 rng(SEED);
for (uint64_t i = 0; i < COUNT; ++i)
map.emplace(rng(), i);
std::unordered_map<uint64_t, char> hashmap(map.begin(), map.end());
auto now = getNow();
{
uint64_t sum = 0;
rng.seed(SEED);
for (uint64_t i = 0; i < COUNT; ++i)
sum += map[i];
std::cout << std::format("Sum: {}\n", sum);
}
std::cout << std::format("Map sum: {} seconds\n", ((getNow() - now).count() / 1'000) / 1e6);
now = getNow();
{
uint64_t sum = 0;
rng.seed(SEED);
for (uint64_t i = 0; i < COUNT; ++i)
sum += hashmap[i];
std::cout << std::format("Hashmap sum: {}\n", sum);
}
std::cout << std::format("Hashmap: {} seconds\n", ((getNow() - now).count() / 1'000) / 1e6);
}