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

add LabeledFnTimed with Into implementation

parent 2ef9eb72
Branches
No related tags found
No related merge requests found
Pipeline #9125 passed
......@@ -10,7 +10,8 @@ fn arithmetic(b: &plnk::Bencher) {
let b = rand::thread_rng().gen::<u128>();
plnk::timeit(|| a + b)
},
),
)
.into(),
(
plnk::label! { operation: "subtraction" }.to_string(),
plnk::closure! {
......@@ -18,7 +19,8 @@ fn arithmetic(b: &plnk::Bencher) {
let b = rand::thread_rng().gen::<u128>();
plnk::timeit(|| a - b)
},
),
)
.into(),
(
plnk::label! { operation: "multiplication" }.to_string(),
plnk::closure! {
......@@ -26,7 +28,8 @@ fn arithmetic(b: &plnk::Bencher) {
let b = rand::thread_rng().gen::<u128>();
plnk::timeit(|| a * b)
},
),
)
.into(),
]);
eprintln!("last value: {}", res.unwrap());
......
......@@ -11,6 +11,17 @@ use indicatif::{MultiProgress, ProgressBar, ProgressStyle};
pub type FnTimed<T> = Box<dyn Fn() -> TimeWithValue<T>>;
pub struct LabeledFnTimed<T> {
label: String,
func: FnTimed<T>,
}
impl<T> From<(String, FnTimed<T>)> for LabeledFnTimed<T> {
fn from((label, func): (String, FnTimed<T>)) -> Self {
LabeledFnTimed { label, func }
}
}
#[derive(Debug, Clone)]
/// hold information about the benchmark to run
pub struct Bencher {
......@@ -115,7 +126,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_and_return<O>(&self, benches: Vec<(String, FnTimed<O>)>) -> Option<O> {
pub fn bench_multiple_and_return<O>(&self, benches: Vec<LabeledFnTimed<O>>) -> Option<O> {
let style = ProgressStyle::with_template(
"[{elapsed_precise}] {bar:40.cyan/blue} {pos:>10}/{len:10} {msg}",
)
......@@ -129,14 +140,14 @@ impl Bencher {
let mut res = None;
let mut append = self.append;
for (label, f) in benches.iter() {
for LabeledFnTimed { label, func } in benches.iter() {
let pb =
mpb.add(ProgressBar::new(self.nb_measurements as u64).with_style(style.clone()));
pb.set_message(label.to_string());
let mut times = vec![];
for _ in 0..self.nb_measurements {
let TimeWithValue { t, v } = f();
let TimeWithValue { t, v } = func();
res = Some(v);
times.push(t.as_nanos());
......@@ -156,25 +167,31 @@ impl Bencher {
/// see [`Self::bench_multiple_and_return`]
#[allow(clippy::type_complexity)]
pub fn bench_multiple<O>(&self, benches: Vec<(String, FnTimed<O>)>) {
pub fn bench_multiple<O>(&self, benches: Vec<LabeledFnTimed<O>>) {
self.bench_multiple_and_return(benches);
}
/// see [`Self::bench_multiple_and_return`]
pub fn bench_and_return<O>(&self, label: impl ToString, f: FnTimed<O>) -> Option<O> {
self.bench_multiple_and_return(vec![(label.to_string(), f)])
self.bench_multiple_and_return(vec![LabeledFnTimed {
label: label.to_string(),
func: f,
}])
}
/// see [`Self::bench_multiple_and_return`]
pub fn bench<O>(&self, label: impl ToString, f: FnTimed<O>) {
self.bench_multiple_and_return(vec![(label.to_string(), f)]);
self.bench_multiple_and_return(vec![LabeledFnTimed {
label: label.to_string(),
func: f,
}]);
}
}
#[macro_export]
macro_rules! closure {
($( $body:stmt );* $(;)?) => {
Box::new(move || { $( $body )* })
Box::new(move || { $( $body )* }) as plnk::FnTimed<_>
};
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment