Skip to content
Snippets Groups Projects
Unverified Commit 3a515bf1 authored by STEVAN Antoine's avatar STEVAN Antoine :crab:
Browse files

don't store "rng" in Bencher

now that the closures can capture the outer scope, it's become useless.
parent 165751a4
No related branches found
No related tags found
No related merge requests found
......@@ -11,18 +11,19 @@ cargo add plnk --git https://gitlab.isae-supaero.fr/a.stevan/plnk --tag 0.2.0 #
use rand::Rng;
fn arithmetic(b: &mut 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));
plnk::bench(b, "addition", || plnk::timeit(|| 1 + 2));
plnk::bench(b, "substraction", || plnk::timeit(|| 1 - 2));
plnk::bench(b, "multiplication", || plnk::timeit(|| 2 * 3));
}
fn random(b: &mut plnk::Bencher) {
plnk::bench(b, "sampling", |rng| plnk::timeit(|| rng.gen::<u128>()));
let mut rng = rand::thread_rng();
plnk::bench(b, "sampling", || plnk::timeit(|| rng.gen::<u128>()));
}
fn main() {
const NB_MEASUREMENTS: usize = 10;
let bencher = plnk::Bencher::new(NB_MEASUREMENTS, rand::thread_rng());
let bencher = plnk::Bencher::new(NB_MEASUREMENTS);
arithmetic(&mut bencher.with_name("arithmetic"));
random(&mut bencher.with_name("random"));
......
use rand::Rng;
fn arithmetic(b: &mut 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));
plnk::bench(b, "addition", || plnk::timeit(|| 1 + 2));
plnk::bench(b, "substraction", || plnk::timeit(|| 1 - 2));
plnk::bench(b, "multiplication", || plnk::timeit(|| 2 * 3));
}
fn random(b: &mut plnk::Bencher) {
plnk::bench(b, "sampling", |rng| plnk::timeit(|| rng.gen::<u128>()));
let mut rng = rand::thread_rng();
plnk::bench(b, "sampling", || plnk::timeit(|| rng.gen::<u128>()));
}
fn main() {
const NB_MEASUREMENTS: usize = 10;
let bencher = plnk::Bencher::new(NB_MEASUREMENTS, rand::thread_rng());
let bencher = plnk::Bencher::new(NB_MEASUREMENTS);
arithmetic(&mut bencher.with_name("arithmetic"));
random(&mut bencher.with_name("random"));
......
fn capture(b: &mut plnk::Bencher) {
let outer = 10;
plnk::bench(b, "dummy", |_| {
plnk::bench(b, "dummy", || {
let inner = outer;
plnk::timeit(|| inner + outer)
});
......@@ -8,7 +8,7 @@ fn capture(b: &mut plnk::Bencher) {
fn main() {
const NB_MEASUREMENTS: usize = 10;
let bencher = plnk::Bencher::new(NB_MEASUREMENTS, rand::thread_rng());
let bencher = plnk::Bencher::new(NB_MEASUREMENTS);
capture(&mut bencher.with_name("capture"));
}
......@@ -2,24 +2,19 @@
use std::time::{Duration, Instant};
use rand::{rngs::ThreadRng, RngCore};
/// hold information about the benchmark to run
pub struct Bencher {
/// the number of times each bench should run, the higher this number the lower the variance
nb_measurements: usize,
/// the name of the benchmark
name: String,
/// a random number generator
rng: ThreadRng,
}
impl Bencher {
pub fn new(nb_measurements: usize, rng: ThreadRng) -> Self {
pub fn new(nb_measurements: usize) -> Self {
Self {
nb_measurements,
name: "".to_string(),
rng,
}
}
......@@ -28,7 +23,6 @@ impl Bencher {
Self {
nb_measurements: self.nb_measurements,
name: name.to_string(),
rng: self.rng.clone(),
}
}
}
......@@ -41,7 +35,7 @@ impl Bencher {
/// - the measurements will be printed to STDOUT as JSON
pub fn bench<F>(b: &mut Bencher, label: &str, mut thing: F)
where
F: FnMut(&mut dyn RngCore) -> Duration,
F: FnMut() -> Duration,
{
let mut times = vec![];
for i in 0..b.nb_measurements {
......@@ -53,7 +47,7 @@ where
b.nb_measurements
);
times.push(thing(&mut b.rng).as_nanos());
times.push(thing().as_nanos());
}
eprintln!();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment