-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_map_performance.cpp
More file actions
85 lines (70 loc) · 2.28 KB
/
string_map_performance.cpp
File metadata and controls
85 lines (70 loc) · 2.28 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
83
84
85
#include "Stopwatch.h"
#include "Timer.h"
#include <cstdint>
#include <format>
#include <iostream>
#include <map>
#include <print>
#include <random>
#include <string>
#include <unordered_map>
std::string randomString(std::mt19937_64 &rng, size_t length, std::string_view prefix) {
std::string out(prefix);
if (length < out.size()) {
out.erase(length);
return out;
}
for (size_t i = out.size(); i < length; ++i) {
out.push_back(static_cast<char>(rng()));
}
return out;
}
std::string randomIdentifier(std::mt19937_64 &rng, size_t length) {
std::string out = "base:";
out.push_back(static_cast<char>(rng()));
out.push_back('/');
for (size_t i = out.size(); i < length; ++i) {
out.push_back(static_cast<char>(rng()));
}
return out;
}
inline std::string generateString(std::mt19937_64 &rng, size_t length, std::string_view prefix) {
return randomString(rng, length, prefix);
// return randomIdentifier(rng, length);
}
template <template <typename...> typename M, size_t Length, bool Prefixed, uint64_t Seed = 64>
uint64_t test(std::string_view name, uint64_t count) {
M<std::string, std::string> map;
std::mt19937_64 rng(Seed);
for (uint64_t i = 0; i < count; ++i) {
std::string string = generateString(rng, Length, Prefixed? "" : std::string_view{});
Timer timer{"{}: emplace", name};
map[string] = std::move(string);
}
volatile uint64_t sum = 0;
Timer timer{"{}: iterate", name};
for (const auto &[key, value]: map) {
sum += value.size();
}
return sum;
}
int main() {
constexpr uint_fast64_t SEED = 64;
constexpr uint64_t COUNT_MAX = 1'000'000;
constexpr size_t LENGTH = 32;
Stopwatch stopwatch;
for (uint64_t count = 1; count <= COUNT_MAX; count *= 10) {
std::println("0/4. Count is \e[1m{}\e[22m.", count);
test<std::map, LENGTH, true, SEED>("std::map (prefixed)", count);
std::println("1/4. {} seconds.", stopwatch.lap());
test<std::unordered_map, LENGTH, true, SEED>("std::unordered_map (prefixed)", count);
std::println("2/4. {} seconds.", stopwatch.lap());
test<std::map, LENGTH, false, SEED>("std::map", count);
std::println("3/4. {} seconds.", stopwatch.lap());
test<std::unordered_map, LENGTH, false, SEED>("std::unordered_map", count);
std::println("4/4. {} seconds.\n", stopwatch.lap());
Timer::summary();
Timer::clear();
std::println();
}
}