From 1fb2aecab3a121d56ae0675e2d73333d169cfd0f Mon Sep 17 00:00:00 2001 From: Trivernis Date: Fri, 3 Apr 2020 16:03:30 +0200 Subject: [PATCH] Subtract the duration it takes to measure from measurements --- Cargo.toml | 2 +- src/benching.rs | 20 +++++++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e4c2ab1..d072ab8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "benchlib-rs" -version = "0.2.2" +version = "0.3.0" authors = ["Trivernis "] edition = "2018" license-file = "LICENSE" diff --git a/src/benching.rs b/src/benching.rs index bcdc219..226f62c 100644 --- a/src/benching.rs +++ b/src/benching.rs @@ -126,17 +126,29 @@ pub struct Bencher { measurements: Vec, iterations: usize, max_auto_iterations: usize, + bench_duration: Duration, } impl Bencher { pub fn new() -> Self { Self { + bench_duration: Self::calculate_bench_duration(), measurements: Vec::new(), iterations: 100, max_auto_iterations: 10000, } } + fn calculate_bench_duration() -> Duration{ + let mut durations = BenchVec::new(); + for _ in 0..1000 { + let start = Instant::now(); + durations.push(start.elapsed()); + } + + durations.average() + } + /// Sets the number of iterations a benchmark will be run /// If set to 0 it iterates until the standard deviation is below 1% pub fn set_iterations(&mut self, iterations: usize) -> &mut Self { @@ -167,7 +179,12 @@ impl Bencher { while count < self.max_auto_iterations { let start = Instant::now(); func(); - durations.push(start.elapsed()); + let duration = start.elapsed(); + if duration > self.bench_duration { + durations.push(duration - self.bench_duration); + } else { + durations.push(duration); + } if (durations.standard_deviation() / durations.average().as_nanos() as f64) < 0.01 && count > 1{ break; } @@ -203,6 +220,7 @@ impl Bencher { /// Prints the settings of the Bencher pub fn print_settings(&mut self) -> &mut Self { println!("\n{}{}Benchmarking Settings{}", color::Fg(color::Green), style::Underline, style::Reset); + println!("Benchmarking accuracy delay:\t {:?}", self.bench_duration); println!("Number of iterations:\t {}", if self.iterations > 0 {self.iterations.to_string() } else { "auto".to_string() }); if self.iterations == 0 { println!("Maximum number of iterations: {}", self.max_auto_iterations)