Skip to content
Snippets Groups Projects
Verified Commit f6592326 authored by STEVAN Antoine's avatar STEVAN Antoine :crab:
Browse files

add `bench`, `bench_multiple` and `bench_and_return`

parent 15ab26aa
Branches
No related tags found
No related merge requests found
Pipeline #9107 passed
......@@ -2,7 +2,7 @@ use clap::Parser;
use rand::Rng;
fn arithmetic(b: &plnk::Bencher) {
let res = b.bench_multiple(vec![
let res = b.bench_multiple_and_return(vec![
(
plnk::label! { operation: "addition" }.to_string(),
plnk::closure! {
......
......@@ -113,7 +113,7 @@ impl Bencher {
/// - the measurements will be printed to STDOUT or to file as JSON
/// - the result of the last run will be returned
#[allow(clippy::type_complexity)]
pub fn bench_multiple<O>(
pub fn bench_multiple_and_return<O>(
&self,
benches: Vec<(String, Box<dyn Fn() -> (Duration, O)>)>,
) -> Option<O> {
......@@ -155,15 +155,24 @@ impl Bencher {
res
}
/// benchmark a piece of code
///
/// - label: an additional label to differentiate similar but different things to benchmark in the
/// same bencher
/// - f: the piece of code to run and measure
/// - the measurements will be printed to STDOUT or to file as JSON
/// - the result of the last run will be returned
pub fn bench<O>(&self, label: impl ToString, f: Box<dyn Fn() -> (Duration, O)>) -> Option<O> {
self.bench_multiple(vec![(label.to_string(), f)])
/// see [`Self::bench_multiple_and_return`]
#[allow(clippy::type_complexity)]
pub fn bench_multiple<O>(&self, benches: Vec<(String, Box<dyn Fn() -> (Duration, O)>)>) {
self.bench_multiple_and_return(benches);
}
/// see [`Self::bench_multiple_and_return`]
pub fn bench_and_return<O>(
&self,
label: impl ToString,
f: Box<dyn Fn() -> (Duration, O)>,
) -> Option<O> {
self.bench_multiple_and_return(vec![(label.to_string(), f)])
}
/// see [`Self::bench_multiple_and_return`]
pub fn bench<O>(&self, label: impl ToString, f: Box<dyn Fn() -> (Duration, O)>) {
self.bench_multiple_and_return(vec![(label.to_string(), f)]);
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment