| 1 |
//! This file contains benchmarks for the ops traits implemented by HashSet. |
| 2 |
//! Each test is intended to have a defined larger and smaller set, |
| 3 |
//! but using a larger size for the "small" set works just as well. |
| 4 |
//! |
| 5 |
//! Each assigning test is done in the configuration that is faster. Cheating, I know. |
| 6 |
//! The exception to this is Sub, because there the result differs. So I made two benchmarks for Sub. |
| 7 |
use criterion::Criterion; |
| 8 |
use hashbrown::HashSet; |
| 9 |
|
| 10 |
/// The number of items to generate for the larger of the sets. |
| 11 |
const LARGE_SET_SIZE: usize = 1000; |
| 12 |
|
| 13 |
/// The number of items to generate for the smaller of the sets. |
| 14 |
const SMALL_SET_SIZE: usize = 100; |
| 15 |
|
| 16 |
/// The number of keys present in both sets. |
| 17 |
const OVERLAP: usize = |
| 18 |
[LARGE_SET_SIZE, SMALL_SET_SIZE][(LARGE_SET_SIZE < SMALL_SET_SIZE) as usize] / 2; |
| 19 |
|
| 20 |
/// Creates a set containing end - start unique string elements. |
| 21 |
fn create_set(start: usize, end: usize) -> HashSet<String> { |
| 22 |
(start..end).map(|nr| format!("key{nr}")).collect() |
| 23 |
} |
| 24 |
|
| 25 |
pub(crate) fn register_benches(c: &mut Criterion) { |
| 26 |
let large_set = create_set(0, LARGE_SET_SIZE); |
| 27 |
let small_set = create_set( |
| 28 |
LARGE_SET_SIZE - OVERLAP, |
| 29 |
LARGE_SET_SIZE + SMALL_SET_SIZE - OVERLAP, |
| 30 |
); |
| 31 |
|
| 32 |
c.bench_function("set_ops_bit_or", |b| { |
| 33 |
b.iter(|| &large_set | &small_set); |
| 34 |
}); |
| 35 |
|
| 36 |
c.bench_function("set_ops_bit_and", |b| { |
| 37 |
b.iter(|| &large_set & &small_set); |
| 38 |
}); |
| 39 |
|
| 40 |
c.bench_function("set_ops_bit_xor", |b| { |
| 41 |
b.iter(|| &large_set ^ &small_set); |
| 42 |
}); |
| 43 |
|
| 44 |
c.bench_function("set_ops_sub_large_small", |b| { |
| 45 |
b.iter(|| &large_set - &small_set); |
| 46 |
}); |
| 47 |
|
| 48 |
c.bench_function("set_ops_sub_small_large", |b| { |
| 49 |
b.iter(|| &small_set - &large_set); |
| 50 |
}); |
| 51 |
|
| 52 |
c.bench_function("set_ops_bit_or_assign", |b| { |
| 53 |
b.iter(|| { |
| 54 |
let mut set = large_set.clone(); |
| 55 |
set |= &small_set; |
| 56 |
set |
| 57 |
}); |
| 58 |
}); |
| 59 |
|
| 60 |
c.bench_function("set_ops_bit_and_assign", |b| { |
| 61 |
b.iter(|| { |
| 62 |
let mut set = small_set.clone(); |
| 63 |
set &= &large_set; |
| 64 |
set |
| 65 |
}); |
| 66 |
}); |
| 67 |
|
| 68 |
c.bench_function("set_ops_bit_xor_assign", |b| { |
| 69 |
b.iter(|| { |
| 70 |
let mut set = large_set.clone(); |
| 71 |
set ^= &small_set; |
| 72 |
set |
| 73 |
}); |
| 74 |
}); |
| 75 |
|
| 76 |
c.bench_function("set_ops_sub_assign_large_small", |b| { |
| 77 |
b.iter(|| { |
| 78 |
let mut set = large_set.clone(); |
| 79 |
set -= &small_set; |
| 80 |
set |
| 81 |
}); |
| 82 |
}); |
| 83 |
|
| 84 |
c.bench_function("set_ops_sub_assign_small_large", |b| { |
| 85 |
b.iter(|| { |
| 86 |
let mut set = small_set.clone(); |
| 87 |
set -= &large_set; |
| 88 |
set |
| 89 |
}); |
| 90 |
}); |
| 91 |
} |