diff --git a/Cargo.toml b/Cargo.toml index 4787169..3843091 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "benchlib-rs" -version = "0.1.2" +version = "0.2.0" authors = ["Trivernis "] edition = "2018" license-file = "LICENSE" diff --git a/src/benching.rs b/src/benching.rs index fe1f89f..1f8f71d 100644 --- a/src/benching.rs +++ b/src/benching.rs @@ -125,15 +125,15 @@ impl Display for DurationDifference { pub struct Bencher { measurements: Vec, iterations: usize, + max_auto_iterations: usize, } -const MAX_AUTO_ITERATIONS: usize = 10000; - impl Bencher { pub fn new() -> Self { Self { measurements: Vec::new(), iterations: 100, + max_auto_iterations: 10000, } } @@ -145,6 +145,12 @@ impl Bencher { self } + pub fn set_max_iterations(&mut self, iterations: usize) -> &mut Self { + self.max_auto_iterations = iterations; + + self + } + /// Benchmarks a closure a configured number of times. /// The result will be printed to the console with the given name. pub fn bench T>(&mut self, name: &str, mut func: F) -> &mut Self { @@ -158,7 +164,7 @@ impl Bencher { ); if self.iterations == 0 { let mut count = 0; - while count < MAX_AUTO_ITERATIONS { + while count < self.max_auto_iterations { let start = Instant::now(); func(); durations.push(start.elapsed()); @@ -167,6 +173,7 @@ impl Bencher { } count += 1; } + println!("{}After {} iterations{}", style::Faint, count, style::Reset); } else { for _ in 0..self.iterations { let start = Instant::now(); diff --git a/src/lib.rs b/src/lib.rs index 02c315c..5f8c2bb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -25,7 +25,7 @@ mod tests { fn bench_auto() { let mut bencher = Bencher::new(); let mut count = 0; - bencher.set_iterations(0); + bencher.set_iterations(0).set_max_iterations(1000); bencher.bench("lol", || count += 1); assert!(count > 1); }