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

add label! and sample random numbers in exemple

parent 93faa423
Branches
Tags
No related merge requests found
......@@ -2,9 +2,23 @@ use clap::Parser;
use rand::Rng;
fn arithmetic(b: &plnk::Bencher) {
plnk::bench(b, "addition", || plnk::timeit(|| 1 + 2));
plnk::bench(b, "substraction", || plnk::timeit(|| 1 - 2));
plnk::bench(b, "multiplication", || plnk::timeit(|| 2 * 3));
let mut rng = rand::thread_rng();
plnk::bench(b, plnk::label! { operation: "addition" }, || {
let a = rng.gen::<u128>();
let b = rng.gen::<u128>();
plnk::timeit(|| a + b)
});
plnk::bench(b, plnk::label! { operation: "substraction" }, || {
let a = rng.gen::<u128>();
let b = rng.gen::<u128>();
plnk::timeit(|| a - b)
});
plnk::bench(b, plnk::label! { operation: "multiplication" }, || {
let a = rng.gen::<u128>();
let b = rng.gen::<u128>();
plnk::timeit(|| a * b)
});
}
fn random(b: &plnk::Bencher) {
......
......@@ -29,6 +29,17 @@ impl Bencher {
}
}
#[macro_export]
macro_rules! label {
( $( $key:ident : $value:expr ),* $(,)? ) => {{
let mut parts = Vec::new();
$(
parts.push(format!("{}: {:?}", stringify!($key), $value));
)*
&format!("{{{}}}", parts.join(", "))
}};
}
/// run a given piece of code a bunch of times and output the measurements
///
/// - label: an additional label to differentiate similar but different things to benchmark in the
......@@ -50,10 +61,7 @@ where
pb.inc(1);
}
println!(
r#"{{"label": "{}", "name": "{}", "times": {:?}}}"#,
label, b.name, times
);
println!("{}", label! { label: label, name: b.name, times: times });
}
/// measure the time it takes to do something
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment