|
|
|
@ -125,15 +125,15 @@ impl Display for DurationDifference {
|
|
|
|
|
pub struct Bencher {
|
|
|
|
|
measurements: Vec<BenchVec>,
|
|
|
|
|
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, F: FnMut() -> 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();
|
|
|
|
|