aboutsummaryrefslogtreecommitdiff
path: root/primrose/crates/candelabra-benchmarker/src/indexable.rs
diff options
context:
space:
mode:
Diffstat (limited to 'primrose/crates/candelabra-benchmarker/src/indexable.rs')
-rw-r--r--primrose/crates/candelabra-benchmarker/src/indexable.rs109
1 files changed, 109 insertions, 0 deletions
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);
+ },
+ |_| (),
+ )
+ }
+}