|
1 | 1 | use benchlib;
|
2 |
| -use benchlib::benchmark::run_benchmark_group; |
| 2 | +use benchlib::benchmark::{black_box, run_benchmark_group}; |
3 | 3 |
|
4 | 4 | fn main() {
|
5 | 5 | 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; |
8 | 9 | let mut map = hashbrown::HashMap::with_capacity_and_hasher(
|
9 |
| - 10000, |
| 10 | + // Over allocate the hashmap to avoid reallocations when inserting |
| 11 | + count * 2, |
10 | 12 | fxhash::FxBuildHasher::default(),
|
11 | 13 | );
|
12 | 14 | move || {
|
13 |
| - for index in 0..10000 { |
| 15 | + for index in 0..count { |
14 | 16 | map.insert(index, index);
|
15 | 17 | }
|
16 | 18 | }
|
17 | 19 | });
|
| 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 | + }); |
18 | 84 | });
|
19 | 85 | }
|
0 commit comments