From 643b898456e4b9d87ad4650c70d8c051a73342fe Mon Sep 17 00:00:00 2001 From: Riccardo Mazzarini Date: Sat, 21 Oct 2023 00:18:33 +0200 Subject: [PATCH] tweak benches --- benches/common.rs | 98 ++++++++++++++++++++++++++--------------------- 1 file changed, 55 insertions(+), 43 deletions(-) diff --git a/benches/common.rs b/benches/common.rs index f5d93e7..94ff9d4 100644 --- a/benches/common.rs +++ b/benches/common.rs @@ -18,33 +18,7 @@ pub trait FromStr<'a> { fn from_str(s: &'a str) -> Self; } -#[inline] -fn bench<'a, M, C>( - group: &mut BenchmarkGroup, - id: BenchmarkId, - metric: &M, - query: &str, - candidates: C, -) where - M: Metric, - C: IntoIterator, - C::IntoIter: ExactSizeIterator + Clone, -{ - let candidates = candidates.into_iter(); - - group.throughput(Throughput::Elements(candidates.len() as u64)); - - group.bench_function(id, |b| { - let query = M::Query::from_str(query); - - b.iter(|| { - for candidate in candidates.clone() { - metric.dist(query, candidate); - } - }) - }); -} - +// TODO: docs fn param( case: CaseSensitivity, with_ranges: bool, @@ -72,32 +46,70 @@ fn param( s } -#[inline] -pub fn short( +// TODO: docs +fn for_all_cases_and_ranges( mut metric: M, suffix: Option<&str>, - mut group: BenchmarkGroup, + mut fun: F, ) where M: Metric, + F: FnMut(&M, BenchmarkId), { for case in [ CaseSensitivity::Sensitive, CaseSensitivity::Insensitive, CaseSensitivity::Smart, ] { - for ranges in [true, false] { - let id = BenchmarkId::new("short", param(case, ranges, suffix)); - - metric = - metric.with_case_sensitivity(case).with_matched_ranges(ranges); - - bench( - &mut group, - id, - &metric, - "jelly", - core::iter::once("jellyfish"), - ); + for with_ranges in [true, false] { + metric = metric + .with_case_sensitivity(case) + .with_matched_ranges(with_ranges); + + let param = param(case, with_ranges, suffix); + + fun(&metric, BenchmarkId::new("short", param)); } } } + +// TODO: docs +fn bench<'a, M, C>( + group: &mut BenchmarkGroup, + id: BenchmarkId, + metric: &M, + query: &str, + candidates: C, +) where + M: Metric, + C: IntoIterator, + C::IntoIter: ExactSizeIterator + Clone, +{ + let query = M::Query::from_str(query); + + let candidates = candidates.into_iter(); + + group.throughput(Throughput::Elements(candidates.len() as u64)); + + group.bench_function(id, |b| { + b.iter(|| { + for candidate in candidates.clone() { + metric.dist(query, candidate); + } + }) + }); +} + +// TODO: docs +pub fn short( + metric: M, + suffix: Option<&str>, + mut group: BenchmarkGroup, +) where + M: Metric, +{ + for_all_cases_and_ranges(metric, suffix, |metric, id| { + let query = "jelly"; + let candidates = core::iter::once("jellyfish"); + bench(&mut group, id, metric, query, candidates); + }) +}