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

move stuff to struct methods

parent b2166613
No related branches found
No related tags found
No related merge requests found
......@@ -2,9 +2,7 @@ use clap::Parser;
use rand::Rng;
fn arithmetic(b: &plnk::Bencher) {
let res = plnk::bench_multiple(
b,
vec![
let res = b.bench_multiple(vec![
(
plnk::label! { operation: "addition" }.to_string(),
Box::new(|| {
......@@ -29,15 +27,13 @@ fn arithmetic(b: &plnk::Bencher) {
plnk::timeit(|| a * b)
}),
),
],
);
]);
eprintln!("last value: {}", res.unwrap());
}
fn random(b: &plnk::Bencher) {
plnk::bench(
b,
b.bench(
plnk::label! { operation: "sampling" },
Box::new(|| plnk::timeit(|| rand::thread_rng().gen::<u128>())),
);
......
......@@ -97,18 +97,6 @@ impl Bencher {
println!("{}", output);
}
}
}
#[macro_export]
macro_rules! label {
( $( $key:ident : $value:expr ),* $(,)? ) => {{
let mut parts = Vec::new();
$(
parts.push(format!("{}: {:?}", stringify!($key), $value));
)*
&format!("{{{}}}", parts.join(", "))
}};
}
/// benchmark multiple pieces of code
///
......@@ -120,7 +108,7 @@ macro_rules! label {
/// - the result of the last run will be returned
#[allow(clippy::type_complexity)]
pub fn bench_multiple<O>(
b: &Bencher,
&self,
benches: Vec<(String, Box<dyn Fn() -> (Duration, O)>)>,
) -> Option<O> {
let style = ProgressStyle::with_template(
......@@ -132,16 +120,17 @@ pub fn bench_multiple<O>(
let mpb = MultiProgress::new();
let pb_main = mpb.add(ProgressBar::new(benches.len() as u64).with_style(style.clone()));
pb_main.set_message(b.name.clone());
pb_main.set_message(self.name.clone());
let mut res = None;
let mut append = b.append;
let mut append = self.append;
for (label, f) in benches.iter() {
let pb = mpb.add(ProgressBar::new(b.nb_measurements as u64).with_style(style.clone()));
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..b.nb_measurements {
for _ in 0..self.nb_measurements {
let (t, _res) = f();
res = Some(_res);
times.push(t.as_nanos());
......@@ -151,11 +140,11 @@ pub fn bench_multiple<O>(
pb_main.inc(1);
pb.finish_and_clear();
b.dump(label, &times, append);
self.dump(label, &times, append);
append = true;
}
pb_main.finish_with_message(format!("{} done", b.name));
pb_main.finish_with_message(format!("{} done", self.name));
res
}
......@@ -167,8 +156,20 @@ pub fn bench_multiple<O>(
/// - 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>(b: &Bencher, label: impl ToString, f: Box<dyn Fn() -> (Duration, O)>) -> Option<O> {
bench_multiple(b, vec![(label.to_string(), f)])
pub fn bench<O>(&self, label: impl ToString, f: Box<dyn Fn() -> (Duration, O)>) -> Option<O> {
self.bench_multiple(vec![(label.to_string(), f)])
}
}
#[macro_export]
macro_rules! label {
( $( $key:ident : $value:expr ),* $(,)? ) => {{
let mut parts = Vec::new();
$(
parts.push(format!("{}: {:?}", stringify!($key), $value));
)*
&format!("{{{}}}", parts.join(", "))
}};
}
/// measure the time it takes to do something, and return the result
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment