Struct criterion::Bencher
[−]
[src]
pub struct Bencher { /* fields omitted */ }Helper struct to time routines
This struct provides different "timing loops" as methods. Each timing loop provides a different way to time a routine and each has advantages and disadvantages.
Methods
impl Bencher[src]
fn iter<O, R>(&mut self, routine: R) where
R: FnMut() -> O, [src]
R: FnMut() -> O,
Times a routine by executing it many times and timing the total elapsed time.
Prefer this timing loop when routine returns a value that doesn't have a destructor.
Timing loop
let start = Instant::now(); for _ in 0..iters { routine(); } let elapsed = start.elapsed();
Timing model
Note that the Bencher also times the time required to destroy the output of routine().
Therefore prefer this timing loop when the runtime of mem::drop(O) is negligible compared
to the runtime of the routine.
elapsed = Instant::now + iters * (routine + mem::drop(O) + Range::next)
NOTE Bencher will choose iters to make Instant::now negligible compared to the product
on the RHS.
fn iter_with_setup<I, O, S, R>(&mut self, setup: S, routine: R) where
S: FnMut() -> I,
R: FnMut(I) -> O, [src]
S: FnMut() -> I,
R: FnMut(I) -> O,
Times a routine that requires some setup on each iteration.
For example, use this loop to benchmark sorting algorithms because they require unsorted data on each iteration.
Example
extern crate criterion; use criterion::Bencher; fn create_scrambled_data() -> Vec<u64> { // ... } // The sorting algorithm to test fn sort(data: &mut [u64]) { // ... } fn benchmark(b: &mut Bencher) { let data = create_scrambled_data(); b.iter_with_setup(move || data.to_vec(), |mut data| sort(&mut data)) }
Timing loop
let mut elapsed = Duration::new(0, 0); for _ in 0..iters { let input = setup(); let start = Instant::now(); let output = routine(input); let elapsed_in_iter = start.elapsed(); mem::drop(output); elapsed = elapsed + elapsed_in_iter; }
Timing model
Note that Bencher also times the Instant::now function. Criterion will warn you (NOTE
not yet implemented) if the runtime of routine is small or comparable to the runtime of
Instant::now as this indicates that the measurement is useless.
elapsed = iters * (Instant::now + routine)
fn iter_with_large_drop<O, R>(&mut self, routine: R) where
R: FnMut() -> O, [src]
R: FnMut() -> O,
Times a routine by collecting its output on each iteration. This avoids timing the
destructor of the value returned by routine.
WARNING: This requires iters * mem::size_of::<O>() of memory, and iters is not under the
control of the caller.
Timing loop
let mut outputs = Vec::with_capacity(iters); let start = Instant::now(); for _ in 0..iters { outputs.push(routine()); } let elapsed = start.elapsed(); mem::drop(outputs);
Timing model
elapsed = Instant::now + iters * (routine + Vec::push + Range::next)
NOTE Bencher will pick an iters that makes Instant::now negligible compared to the
product on the RHS. Vec::push will never incur in a re-allocation because its capacity is
pre-allocated.
fn iter_with_large_setup<I, S, R>(&mut self, setup: S, routine: R) where
S: FnMut() -> I,
R: FnMut(I), [src]
S: FnMut() -> I,
R: FnMut(I),
Times a routine that needs to consume its input by first creating a pool of inputs.
This function is handy for benchmarking destructors.
WARNING This requires iters * mem::size_of::<I>() of memory, and iters is not under the
control of the caller.
Timing loop
let inputs: Vec<()> = (0..iters).map(|_| setup()).collect(); let start = Instant::now(); for input in inputs { routine(input); } let elapsed = start.elapsed();
Timing model
elapsed = Instant::now + iters * (routine + vec::IntoIter::next)
Trait Implementations
impl Clone for Bencher[src]
fn clone(&self) -> Bencher[src]
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)1.0.0[src]
Performs copy-assignment from source. Read more