diff options
Diffstat (limited to 'primrose/crates')
-rw-r--r-- | primrose/crates/candelabra-benchmarker/examples/run_vec.rs | 4 | ||||
-rw-r--r-- | primrose/crates/candelabra-benchmarker/src/indexable.rs | 109 | ||||
-rw-r--r-- | primrose/crates/candelabra-benchmarker/src/lib.rs | 3 |
3 files changed, 114 insertions, 2 deletions
diff --git a/primrose/crates/candelabra-benchmarker/examples/run_vec.rs b/primrose/crates/candelabra-benchmarker/examples/run_vec.rs index e0fd068..677a538 100644 --- a/primrose/crates/candelabra-benchmarker/examples/run_vec.rs +++ b/primrose/crates/candelabra-benchmarker/examples/run_vec.rs @@ -1,9 +1,9 @@ -use candelabra_benchmarker::ContainerExt; +use candelabra_benchmarker::{ContainerExt, IndexableExt}; const NS: [usize; 3] = [1, 10, 100]; fn main() { env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("debug")).init(); - dbg!(Vec::<usize>::benchmark_container(&NS)); + dbg!(Vec::<usize>::benchmark_container(&NS).merge(Vec::<usize>::benchmark_indexable(&NS))); } diff --git a/primrose/crates/candelabra-benchmarker/src/indexable.rs b/primrose/crates/candelabra-benchmarker/src/indexable.rs new file mode 100644 index 0000000..122ec58 --- /dev/null +++ b/primrose/crates/candelabra-benchmarker/src/indexable.rs @@ -0,0 +1,109 @@ +use std::{any::type_name, collections::HashMap}; + +use log::debug; +use primrose_library::traits::{Container, Indexable}; +use rand::{distributions::Standard, prelude::Distribution, random}; + +use crate::{benchmark_op, Results, RunResults, SingleNResults}; + +pub trait IndexableExt<E> { + /// Benchmark at the given `ns`. + fn benchmark_indexable(ns: &[usize]) -> Results; + + /// Benchmark at a single `n`. + fn benchmark_indexable_at(n: usize) -> SingleNResults; + + /// Benchmark `first` at a single `n`. + fn benchmark_indexable_first(n: usize) -> RunResults; + + /// Benchmark `last` at a single `n`. + fn benchmark_indexable_last(n: usize) -> RunResults; + + /// Benchmark `nth` at a single `n`. + fn benchmark_indexable_nth(n: usize) -> RunResults; +} + +impl<T, E> IndexableExt<E> for T +where + T: Container<E> + Indexable<E> + Default, + Standard: Distribution<E>, +{ + fn benchmark_indexable(ns: &[usize]) -> Results { + debug!( + "Benchmarking {} at {} different n values", + type_name::<T>(), + ns.len() + ); + + let mut by_n = HashMap::new(); + for n in ns { + by_n.insert(*n, Self::benchmark_indexable_at(*n)); + } + + Results { by_n } + } + + fn benchmark_indexable_at(n: usize) -> SingleNResults { + let mut by_op = HashMap::new(); + + debug!("Benchmarking {} at n = {}", type_name::<T>(), n); + + debug!("...first"); + by_op.insert("first", Self::benchmark_indexable_first(n)); + debug!("...last"); + by_op.insert("last", Self::benchmark_indexable_last(n)); + debug!("...nth"); + by_op.insert("nth", Self::benchmark_indexable_nth(n)); + debug!("--- done!"); + + SingleNResults { by_op } + } + + fn benchmark_indexable_first(n: usize) -> RunResults { + benchmark_op( + || { + let mut c = T::default(); + for _ in 0..n { + c.insert(random()); + } + c + }, + |c| { + c.first(); + }, + |_| (), + ) + } + + fn benchmark_indexable_last(n: usize) -> RunResults { + benchmark_op( + || { + let mut c = T::default(); + for _ in 0..n { + c.insert(random()); + } + c + }, + |c| { + c.last(); + }, + |_| (), + ) + } + + fn benchmark_indexable_nth(n: usize) -> RunResults { + benchmark_op( + || { + let mut c = T::default(); + for _ in 0..n { + c.insert(random()); + } + (c, random::<usize>()) + }, + |(c, fetch)| { + c.nth(*fetch); + }, + |_| (), + ) + } +} diff --git a/primrose/crates/candelabra-benchmarker/src/lib.rs b/primrose/crates/candelabra-benchmarker/src/lib.rs index c69d699..87fee8d 100644 --- a/primrose/crates/candelabra-benchmarker/src/lib.rs +++ b/primrose/crates/candelabra-benchmarker/src/lib.rs @@ -6,6 +6,9 @@ pub use bench::benchmark_op; mod container; pub use container::ContainerExt; +mod indexable; +pub use indexable::IndexableExt; + /// Results for a whole suite of benchmarks #[derive(Debug, Clone)] pub struct Results { |