diff options
author | Aria <me@aria.rip> | 2023-11-28 14:14:36 +0000 |
---|---|---|
committer | Aria <me@aria.rip> | 2023-11-28 14:14:36 +0000 |
commit | b73cf91fd6debef13fe7ec9081f27c49a4707cd3 (patch) | |
tree | 23b5b7d4973cdf2730f7ced8af089986e086917e /src/crates/benchmarker | |
parent | 328cd91bb845b9817772754966814c4d440d610a (diff) |
fix(benchmarker): WIP rest of container criterion benchmarks
Diffstat (limited to 'src/crates/benchmarker')
-rw-r--r-- | src/crates/benchmarker/src/container.rs | 137 |
1 files changed, 51 insertions, 86 deletions
diff --git a/src/crates/benchmarker/src/container.rs b/src/crates/benchmarker/src/container.rs index 75182a8..9da58d6 100644 --- a/src/crates/benchmarker/src/container.rs +++ b/src/crates/benchmarker/src/container.rs @@ -1,116 +1,81 @@ use criterion::{black_box, BatchSize, Bencher, BenchmarkId, Criterion}; -use primrose_library::traits::Container; +use primrose_library::traits::{Container, Indexable}; use rand::{distributions::Standard, prelude::Distribution, random, thread_rng, Rng}; pub fn benchmark_container<T, E>(c: &mut Criterion, ns: &[usize]) where - T: Container<E> + Default + Clone, + T: Container<E> + Indexable<E> + Default + Clone, E: Clone, Standard: Distribution<E>, { - // bench_with_ns(c, ns, "contains", |b, &n| { - // b.iter_batched_ref( - // || { - // // TODO: maybe we should actually just test the worst case? (at the end) - // // we also don't actually test misses yet. - - // let mut rng = thread_rng(); - // let mut c = T::default(); - - // // decide where the element that we will search for will be - // let pivot = rng.gen_range(0..n); - - // // insert the element at pivot, and keep track of what it is - // for _ in 0..pivot { - // c.insert(random()); - // } - // let chosen = rng.gen(); - // c.insert(chosen.clone()); - // for _ in pivot..n { - // c.insert(random()); - // } + bench_with_ns::<T, E>(c, ns, "contains", |b, container| { + b.iter_batched_ref( + || { + // TODO: maybe we should actually just test the worst case? (at the end) + // we also don't actually test misses yet. + let mut container = container.clone(); + let mut rng = thread_rng(); + let pivot = rng.gen_range(0..container.len()); + let chosen = container.nth(pivot).unwrap().clone(); - // (c, chosen) - // }, - // |(c, search)| c.contains(search), - // BatchSize::SmallInput, - // ); - // }); + (container, chosen) + }, + |(c, search)| c.contains(search), + BatchSize::LargeInput, + ); + }); - bench_with_ns(c, ns, "len", |b, container| { + bench_with_ns::<T, E>(c, ns, "len", |b, container| { b.iter_batched_ref( || black_box(container.clone()), |c| black_box(c.len()), - BatchSize::SmallInput, + BatchSize::LargeInput, ); }); - // bench_with_ns(c, ns, "insert", |b, &n| { - // b.iter_batched_ref( - // || { - // let mut c = T::default(); - // for _ in 0..n { - // c.insert(random()); - // } - // c - // }, - // |c| c.insert(random()), - // BatchSize::SmallInput, - // ); - // }); - - // bench_with_ns(c, ns, "remove", |b, &n| { - // b.iter_batched_ref( - // || { - // let mut rng = thread_rng(); - // let mut c = T::default(); - - // // decide where the element that we will remove will be - // let pivot = rng.gen_range(0..n); + bench_with_ns::<T, E>(c, ns, "insert", |b, container| { + b.iter_batched_ref( + || container.clone(), + |c| c.insert(random()), + BatchSize::LargeInput, + ); + }); - // // insert the element at pivot, and keep track of what it is - // for _ in 0..pivot { - // c.insert(random()); - // } - // let chosen = rng.gen(); - // c.insert(chosen.clone()); - // for _ in pivot..n { - // c.insert(random()); - // } + bench_with_ns::<T, E>(c, ns, "remove", |b, container| { + b.iter_batched_ref( + || { + let mut container = container.clone(); + let mut rng = thread_rng(); + let pivot = rng.gen_range(0..container.len()); + let chosen = container.nth(pivot).unwrap().clone(); - // (c, chosen) - // }, - // |(c, chosen)| c.remove(chosen.clone()), - // BatchSize::SmallInput, - // ); - // }); + (container, chosen) + }, + |(c, chosen)| c.remove(chosen.clone()), + BatchSize::LargeInput, + ); + }); - // bench_with_ns(c, ns, "clear", |b, &n| { - // b.iter_batched_ref( - // || { - // let mut c = T::default(); - // for _ in 0..n { - // c.insert(random()); - // } - // c - // }, - // |c| c.clear(), - // BatchSize::SmallInput, - // ); - // }); + bench_with_ns::<T, E>(c, ns, "clear", |b, container| { + b.iter_batched_ref(|| container.clone(), |c| c.clear(), BatchSize::LargeInput); + }); } -fn bench_with_ns( +fn bench_with_ns<T, E>( c: &mut Criterion, ns: &[usize], name: &str, - mut f: impl FnMut(&mut Bencher<'_>, &Vec<usize>) -> (), -) { + mut f: impl FnMut(&mut Bencher<'_>, &T) -> (), +) where + T: Container<E> + Default + Clone, + E: Clone, + Standard: Distribution<E>, +{ let mut g = c.benchmark_group(name); for n in ns { - let mut container: Vec<usize> = Vec::default(); + let mut container = T::default(); for _ in 0..*n { - container.push(random::<usize>()); + container.insert(random::<E>()); } g.bench_with_input(BenchmarkId::from_parameter(n), &container, |b, n| f(b, n)); |