-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathbenchmark.rs
188 lines (178 loc) · 4.79 KB
/
benchmark.rs
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
// SPDX-FileCopyrightText: Copyright (c) 2023-2025 Yegor Bugayenko
// SPDX-License-Identifier: MIT
// In order to run this single test from the command line:
// $ cargo test --test benchmark -- --nocapture
use std::collections::HashMap;
use std::env;
use std::time::{Duration, Instant};
const CAPACITY: usize = 1;
macro_rules! eval {
($map:expr, $total:expr, $capacity:expr) => {{
let mut sum = 0;
for _ in 0..$total {
$map.clear();
let _ = $map.insert(0, 42);
for i in 1..$capacity - 1 {
let _ = $map.insert(i as u32, i as i64);
let v = std::hint::black_box(*$map.get(&(i as u32)).unwrap());
assert_eq!(v, i as i64);
}
for i in 1..$capacity - 1 {
// for [`indexmap::IndexMap`]`, no logic changes. (or use .swap_remove(key))
#[allow(deprecated)]
$map.remove(&(i as u32));
}
if $map.iter().find(|(_k, v)| **v == 0).is_some() {
$map.clear();
}
sum += std::hint::black_box($map.iter().find(|(_k, v)| **v == 42).unwrap().1);
}
std::hint::black_box(sum)
}};
}
macro_rules! insert {
($name:expr, $ret:expr, $map:expr, $total:expr) => {{
let start = Instant::now();
let mut m = $map;
eval!(m, $total, CAPACITY);
let e = start.elapsed();
$ret.insert($name, e);
}};
}
macro_rules! insert_flurry {
($name:expr, $ret:expr, $map:expr, $total:expr) => {{
let start = Instant::now();
let m = $map;
eval!(m.pin(), $total, CAPACITY);
let e = start.elapsed();
$ret.insert($name, e);
}};
}
fn benchmark(total: usize) -> HashMap<&'static str, Duration> {
let mut ret = HashMap::new();
insert!(
"std::collections::HashMap",
ret,
HashMap::<u32, i64>::with_capacity(CAPACITY),
total
);
insert!(
"hashbrown::HashMap",
ret,
hashbrown::HashMap::<u32, i64>::new(),
total
);
insert!(
"rustc_hash::FxHashMap",
ret,
rustc_hash::FxHashMap::<u32, i64>::default(),
total
);
insert!(
"nohash_hasher::BuildNoHashHasher",
ret,
HashMap::<u32, i64, nohash_hasher::BuildNoHashHasher<u32>>::with_capacity_and_hasher(
2,
nohash_hasher::BuildNoHashHasher::default()
),
total
);
insert!(
"std::collections::BTreeMap",
ret,
std::collections::BTreeMap::<u32, i64>::new(),
total
);
insert!(
"tinymap::array_map::ArrayMap",
ret,
tinymap::array_map::ArrayMap::<u32, i64, CAPACITY>::new(),
total
);
insert!(
"linked_hash_map::LinkedHashMap",
ret,
linked_hash_map::LinkedHashMap::<u32, i64>::new(),
total
);
insert!(
"linear_map::LinearMap",
ret,
linear_map::LinearMap::<u32, i64>::new(),
total
);
insert!(
"indexmap::IndexMap",
ret,
indexmap::IndexMap::<u32, i64>::new(),
total
);
insert!(
"litemap::LiteMap",
ret,
litemap::LiteMap::<u32, i64>::new(),
total
);
insert!(
"heapless::LinearMap",
ret,
heapless::LinearMap::<u32, i64, CAPACITY>::new(),
total
);
insert_flurry!(
"flurry::HashMap",
ret,
flurry::HashMap::<u32, i64>::new(),
total
);
insert!(
"micromap::Map",
ret,
micromap::Map::<u32, i64, CAPACITY>::new(),
total
);
ret
}
/// Run it like this from the command line:
///
/// ```text
/// $ cargo test --test benchmark -- --include-ignored --nocapture
/// ```
#[test]
#[ignore]
pub fn benchmark_and_print() {
let times = benchmark(
#[cfg(debug_assertions)]
100000,
#[cfg(not(debug_assertions))]
10000000,
);
let ours = times.get("micromap::Map").unwrap();
let mut total_gain = 0.0;
let mut total_loss = 0.0;
for (m, d) in × {
let differential = d.as_nanos() as f64 / ours.as_nanos() as f64;
println!("{m} -> {:?} ({:.2}x)", d, differential);
if d == ours {
continue;
}
if d.cmp(ours).is_gt() {
total_gain += differential;
} else {
total_loss += differential;
}
}
println!("Total gain: {:.2}, loss: {:.2}", total_gain, total_loss);
}
pub fn main() {
let args: Vec<String> = env::args().collect();
let times = benchmark(args.get(1).unwrap().parse::<usize>().unwrap());
let mut lines = vec![];
for (m, d) in × {
lines.push(format!("{m}\t{}", d.as_nanos()));
}
lines.sort();
for t in lines {
println!("{t}");
}
}