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

do not requite closures to be mutable

parent 42a86916
No related branches found
No related tags found
No related merge requests found
......@@ -2,21 +2,19 @@ use clap::Parser;
use rand::Rng;
fn arithmetic(b: &plnk::Bencher) {
let mut rng = rand::thread_rng();
plnk::bench(b, plnk::label! { operation: "addition" }, || {
let a = rng.gen::<u128>();
let b = rng.gen::<u128>();
let a = rand::thread_rng().gen::<u128>();
let b = rand::thread_rng().gen::<u128>();
plnk::timeit(|| a + b)
});
plnk::bench(b, plnk::label! { operation: "substraction" }, || {
let a = rng.gen::<u128>();
let b = rng.gen::<u128>();
let a = rand::thread_rng().gen::<u128>();
let b = rand::thread_rng().gen::<u128>();
plnk::timeit(|| a - b)
});
let res = plnk::bench_and_return(b, plnk::label! { operation: "multiplication" }, || {
let a = rng.gen::<u128>();
let b = rng.gen::<u128>();
let a = rand::thread_rng().gen::<u128>();
let b = rand::thread_rng().gen::<u128>();
plnk::timeit_and_return(|| a * b)
});
......@@ -26,13 +24,13 @@ fn arithmetic(b: &plnk::Bencher) {
fn arithmetic_multiple(b: &plnk::Bencher) {
plnk::bench_multiple(
b,
&mut [
vec![
(
plnk::label! { operation: "addition" }.to_string(),
Box::new(|| {
let a = rand::thread_rng().gen::<u128>();
let b = rand::thread_rng().gen::<u128>();
plnk::timeit(|| a + b)
plnk::timeit(|| a - b)
}),
),
(
......@@ -56,9 +54,8 @@ fn arithmetic_multiple(b: &plnk::Bencher) {
}
fn random(b: &plnk::Bencher) {
let mut rng = rand::thread_rng();
plnk::bench(b, plnk::label! { operation: "sampling" }, || {
plnk::timeit(|| rng.gen::<u128>())
plnk::timeit(|| rand::thread_rng().gen::<u128>())
});
}
......
......@@ -65,9 +65,9 @@ macro_rules! label {
/// same bencher
/// - f: the piece of code to run and measure
/// - the measurements will be printed to STDOUT as JSON
pub fn bench<F>(b: &Bencher, label: &str, mut f: F)
pub fn bench<F>(b: &Bencher, label: &str, f: F)
where
F: FnMut() -> Duration,
F: Fn() -> Duration,
{
if !b.quiet {
eprintln!("bencher: {}, label: {}", b.name, label);
......@@ -93,7 +93,7 @@ where
/// - f: the piece of code to run and measure
/// - the measurements will be printed to STDOUT as JSON
#[allow(clippy::type_complexity)]
pub fn bench_multiple(b: &Bencher, benches: &mut [(String, Box<dyn FnMut() -> Duration>)]) {
pub fn bench_multiple(b: &Bencher, benches: Vec<(String, Box<dyn Fn() -> Duration>)>) {
let style = ProgressStyle::with_template(
"[{elapsed_precise}] {bar:40.cyan/blue} {pos:>10}/{len:10} {msg}",
)
......@@ -105,7 +105,7 @@ pub fn bench_multiple(b: &Bencher, benches: &mut [(String, Box<dyn FnMut() -> Du
let pb_main = mpb.add(ProgressBar::new(benches.len() as u64).with_style(style.clone()));
pb_main.set_message(b.name.clone());
for (label, f) in benches.iter_mut() {
for (label, f) in benches.iter() {
let pb = mpb.add(ProgressBar::new(b.nb_measurements as u64).with_style(style.clone()));
pb.set_message(label.to_string());
......@@ -131,9 +131,9 @@ pub fn bench_multiple(b: &Bencher, benches: &mut [(String, Box<dyn FnMut() -> Du
/// same bencher
/// - f: the piece of code to run and measure
/// - the measurements will be printed to STDOUT as JSON
pub fn bench_and_return<F, O>(b: &Bencher, label: &str, mut f: F) -> Option<O>
pub fn bench_and_return<F, O>(b: &Bencher, label: &str, f: F) -> Option<O>
where
F: FnMut() -> (Duration, O),
F: Fn() -> (Duration, O),
{
if !b.quiet {
eprintln!("bencher: {}, label: {}", b.name, label);
......@@ -164,9 +164,9 @@ where
/// # use plnk::timeit;
/// let t = timeit(|| 1 + 2);
/// ```
pub fn timeit<F, O>(mut f: F) -> Duration
pub fn timeit<F, O>(f: F) -> Duration
where
F: FnMut() -> O,
F: Fn() -> O,
{
let start_time = Instant::now();
let _ = f();
......@@ -181,9 +181,9 @@ where
/// let (t, res) = timeit_and_return(|| 1 + 2);
/// assert_eq!(res, 3);
/// ```
pub fn timeit_and_return<F, O>(mut f: F) -> (Duration, O)
pub fn timeit_and_return<F, O>(f: F) -> (Duration, O)
where
F: FnMut() -> O,
F: Fn() -> O,
{
let start_time = Instant::now();
let res = f();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment