Skip to content

Commit

Permalink
tweak benches
Browse files Browse the repository at this point in the history
  • Loading branch information
noib3 committed Oct 20, 2023
1 parent 6f5ae1f commit 643b898
Showing 1 changed file with 55 additions and 43 deletions.
98 changes: 55 additions & 43 deletions benches/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,7 @@ pub trait FromStr<'a> {
fn from_str(s: &'a str) -> Self;
}

#[inline]
fn bench<'a, M, C>(
group: &mut BenchmarkGroup<WallTime>,
id: BenchmarkId,
metric: &M,
query: &str,
candidates: C,
) where
M: Metric,
C: IntoIterator<Item = &'a str>,
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,
Expand Down Expand Up @@ -72,32 +46,70 @@ fn param(
s
}

#[inline]
pub fn short<M: Metric>(
// TODO: docs
fn for_all_cases_and_ranges<M, F>(
mut metric: M,
suffix: Option<&str>,
mut group: BenchmarkGroup<WallTime>,
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<WallTime>,
id: BenchmarkId,
metric: &M,
query: &str,
candidates: C,
) where
M: Metric,
C: IntoIterator<Item = &'a str>,
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<M: Metric>(
metric: M,
suffix: Option<&str>,
mut group: BenchmarkGroup<WallTime>,
) 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);
})
}

0 comments on commit 643b898

Please sign in to comment.