Subtract the duration it takes to measure from measurements

master
Trivernis 4 years ago
parent d767128309
commit 1fb2aecab3

@ -1,6 +1,6 @@
[package] [package]
name = "benchlib-rs" name = "benchlib-rs"
version = "0.2.2" version = "0.3.0"
authors = ["Trivernis <trivernis@gmail.com>"] authors = ["Trivernis <trivernis@gmail.com>"]
edition = "2018" edition = "2018"
license-file = "LICENSE" license-file = "LICENSE"

@ -126,17 +126,29 @@ pub struct Bencher {
measurements: Vec<BenchVec>, measurements: Vec<BenchVec>,
iterations: usize, iterations: usize,
max_auto_iterations: usize, max_auto_iterations: usize,
bench_duration: Duration,
} }
impl Bencher { impl Bencher {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
bench_duration: Self::calculate_bench_duration(),
measurements: Vec::new(), measurements: Vec::new(),
iterations: 100, iterations: 100,
max_auto_iterations: 10000, 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 /// Sets the number of iterations a benchmark will be run
/// If set to 0 it iterates until the standard deviation is below 1% /// If set to 0 it iterates until the standard deviation is below 1%
pub fn set_iterations(&mut self, iterations: usize) -> &mut Self { pub fn set_iterations(&mut self, iterations: usize) -> &mut Self {
@ -167,7 +179,12 @@ impl Bencher {
while count < self.max_auto_iterations { while count < self.max_auto_iterations {
let start = Instant::now(); let start = Instant::now();
func(); 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{ if (durations.standard_deviation() / durations.average().as_nanos() as f64) < 0.01 && count > 1{
break; break;
} }
@ -203,6 +220,7 @@ impl Bencher {
/// Prints the settings of the Bencher /// Prints the settings of the Bencher
pub fn print_settings(&mut self) -> &mut Self { pub fn print_settings(&mut self) -> &mut Self {
println!("\n{}{}Benchmarking Settings{}", color::Fg(color::Green), style::Underline, style::Reset); 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() }); println!("Number of iterations:\t {}", if self.iterations > 0 {self.iterations.to_string() } else { "auto".to_string() });
if self.iterations == 0 { if self.iterations == 0 {
println!("Maximum number of iterations: {}", self.max_auto_iterations) println!("Maximum number of iterations: {}", self.max_auto_iterations)

Loading…
Cancel
Save