Skip to content

Commit 455e4fa

Browse files
committed
1 parent 7a92590 commit 455e4fa

File tree

3 files changed

+77
-28
lines changed

3 files changed

+77
-28
lines changed

collector/runtime-benchmarks/hashmap/Cargo.lock

Lines changed: 5 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

collector/runtime-benchmarks/hashmap/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ edition = "2021"
77

88
[dependencies]
99
benchlib = { path = "../../benchlib" }
10-
hashbrown = "0.12.3"
10+
hashbrown = { version = "0.13.2" }
1111
fxhash = "0.2.1"
1212

1313
[workspace]
Lines changed: 71 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,85 @@
11
use benchlib;
2-
use benchlib::benchmark::run_benchmark_group;
2+
use benchlib::benchmark::{black_box, run_benchmark_group};
33

44
fn main() {
55
run_benchmark_group(|group| {
6-
// Measures how long does it take to insert 10 thousand numbers into a `hashbrown` hashmap.
7-
group.register_benchmark("hashmap_insert_10k", || {
6+
// Measures how long does it take to insert 1 million numbers into a hashmap.
7+
group.register_benchmark("hashmap_insert_1m", || {
8+
let count = 1_000_000;
89
let mut map = hashbrown::HashMap::with_capacity_and_hasher(
9-
10000,
10+
// Over allocate the hashmap to avoid reallocations when inserting
11+
count * 2,
1012
fxhash::FxBuildHasher::default(),
1113
);
1214
move || {
13-
for index in 0..10000 {
15+
for index in 0..count {
1416
map.insert(index, index);
1517
}
1618
}
1719
});
20+
21+
// Measures how long it takes to remove 1 million elements from a hashmap.
22+
group.register_benchmark("hashmap_remove_1m", || {
23+
let mut map = hashbrown::HashMap::with_capacity_and_hasher(
24+
1_000_000,
25+
fxhash::FxBuildHasher::default(),
26+
);
27+
for index in 0..map.capacity() {
28+
map.insert(index, index);
29+
}
30+
31+
move || {
32+
for index in 0..map.capacity() {
33+
map.remove(&index);
34+
}
35+
}
36+
});
37+
38+
// Measures how long it takes to find 1 million elements that are in a hashmap.
39+
group.register_benchmark("hashmap_find_1m", || {
40+
let mut map = hashbrown::HashMap::with_capacity_and_hasher(
41+
1_000_000,
42+
fxhash::FxBuildHasher::default(),
43+
);
44+
for index in 0..map.capacity() {
45+
map.insert(index, index);
46+
}
47+
48+
move || {
49+
for index in 0..map.capacity() {
50+
black_box(map.get(&index));
51+
}
52+
}
53+
});
54+
55+
// Measures how long it takes to find 1 million elements that are not in a hashmap.
56+
group.register_benchmark("hashmap_find_misses_1m", || {
57+
let mut map = hashbrown::HashMap::with_capacity_and_hasher(
58+
1_000_000,
59+
fxhash::FxBuildHasher::default(),
60+
);
61+
for index in 0..map.capacity() {
62+
map.insert(index, index);
63+
}
64+
65+
move || {
66+
for index in map.capacity()..(map.capacity() * 2) {
67+
black_box(map.get(&index));
68+
}
69+
}
70+
});
71+
72+
// Measures how long it takes to iterate through values of a hashmap with 1 million elements.
73+
group.register_benchmark("hashmap_iterate_1m", || {
74+
let mut map = hashbrown::HashMap::with_capacity_and_hasher(
75+
1_000_000,
76+
fxhash::FxBuildHasher::default(),
77+
);
78+
for index in 0..map.capacity() {
79+
map.insert(index, index as u64);
80+
}
81+
82+
move || map.values().sum::<u64>()
83+
});
1884
});
1985
}

0 commit comments

Comments
 (0)