use criterion::Criterion;
use hashbrown::HashSet;
const LARGE_SET_SIZE: usize = 1000;
const SMALL_SET_SIZE: usize = 100;
const OVERLAP: usize =
[LARGE_SET_SIZE, SMALL_SET_SIZE][(LARGE_SET_SIZE < SMALL_SET_SIZE) as usize] / 2;
fn create_set(start: usize, end: usize) -> HashSet<String> {
(start..end).map(|nr| format!("key{nr}")).collect()
}
pub(crate) fn register_benches(c: &mut Criterion) {
let large_set = create_set(0, LARGE_SET_SIZE);
let small_set = create_set(
LARGE_SET_SIZE - OVERLAP,
LARGE_SET_SIZE + SMALL_SET_SIZE - OVERLAP,
);
c.bench_function("set_ops_bit_or", |b| {
b.iter(|| &large_set | &small_set);
});
c.bench_function("set_ops_bit_and", |b| {
b.iter(|| &large_set & &small_set);
});
c.bench_function("set_ops_bit_xor", |b| {
b.iter(|| &large_set ^ &small_set);
});
c.bench_function("set_ops_sub_large_small", |b| {
b.iter(|| &large_set - &small_set);
});
c.bench_function("set_ops_sub_small_large", |b| {
b.iter(|| &small_set - &large_set);
});
c.bench_function("set_ops_bit_or_assign", |b| {
b.iter(|| {
let mut set = large_set.clone();
set |= &small_set;
set
});
});
c.bench_function("set_ops_bit_and_assign", |b| {
b.iter(|| {
let mut set = small_set.clone();
set &= &large_set;
set
});
});
c.bench_function("set_ops_bit_xor_assign", |b| {
b.iter(|| {
let mut set = large_set.clone();
set ^= &small_set;
set
});
});
c.bench_function("set_ops_sub_assign_large_small", |b| {
b.iter(|| {
let mut set = large_set.clone();
set -= &small_set;
set
});
});
c.bench_function("set_ops_sub_assign_small_large", |b| {
b.iter(|| {
let mut set = small_set.clone();
set -= &large_set;
set
});
});
}