pub struct Bencher { /* private fields */ }
Expand description
Timer struct to iterate a benchmarked function and measure the runtime.
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.
- If your routine requires no per-iteration setup and returns a value with an expensive
drop
method, useiter_with_large_drop
. - If your routine requires some per-iteration setup that shouldn’t be timed, use
iter_batched
oriter_batched_ref
. SeeBatchSize
for a discussion of batch sizes. If the setup value implementsDrop
and you don’t want to include thedrop
time in the measurement, useiter_batched_ref
, otherwise useiter_batched
. These methods are also suitable for benchmarking routines which return a value with an expensivedrop
method, but are more complex thaniter_with_large_drop
. - Otherwise, use
iter
.
Implementations§
Source§impl Bencher
impl Bencher
Sourcepub fn iter<O, R>(&mut self, routine: R)where
R: FnMut() -> O,
pub fn iter<O, R>(&mut self, routine: R)where
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 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)
§Example
#[macro_use] extern crate criterion;
use criterion::*;
// The function to benchmark
fn foo() {
// ...
}
fn bench(c: &mut Criterion) {
c.bench_function("iter", move |b| {
b.iter(|| foo())
});
}
criterion_group!(benches, bench);
criterion_main!(benches);
Sourcepub fn iter_batched<I, O, S, R>(
&mut self,
setup: S,
routine: R,
size: BatchSize,
)
pub fn iter_batched<I, O, S, R>( &mut self, setup: S, routine: R, size: BatchSize, )
Times a routine
that requires some input by generating a batch of input, then timing the
iteration of the benchmark over the input. See BatchSize
for
details on choosing the batch size. Use this when the routine must consume its input.
For example, use this loop to benchmark sorting algorithms, because they require unsorted data on each iteration.
§Timing model
elapsed = (Instant::now * num_batches) + (iters * (routine + O::drop)) + Vec::extend
§Example
#[macro_use] extern crate criterion;
use criterion::*;
fn create_scrambled_data() -> Vec<u64> {
// ...
}
// The sorting algorithm to test
fn sort(data: &mut [u64]) {
// ...
}
fn bench(c: &mut Criterion) {
let data = create_scrambled_data();
c.bench_function("with_setup", move |b| {
// This will avoid timing the to_vec call.
b.iter_batched(|| data.clone(), |mut data| sort(&mut data), BatchSize::SmallInput)
});
}
criterion_group!(benches, bench);
criterion_main!(benches);
Sourcepub fn iter_batched_ref<I, O, S, R>(
&mut self,
setup: S,
routine: R,
size: BatchSize,
)
pub fn iter_batched_ref<I, O, S, R>( &mut self, setup: S, routine: R, size: BatchSize, )
Times a routine
that requires some input by generating a batch of input, then timing the
iteration of the benchmark over the input. See BatchSize
for
details on choosing the batch size. Use this when the routine should accept the input by
mutable reference.
For example, use this loop to benchmark sorting algorithms, because they require unsorted data on each iteration.
§Timing model
elapsed = (Instant::now * num_batches) + (iters * routine) + Vec::extend
§Example
#[macro_use] extern crate criterion;
use criterion::*;
fn create_scrambled_data() -> Vec<u64> {
// ...
}
// The sorting algorithm to test
fn sort(data: &mut [u64]) {
// ...
}
fn bench(c: &mut Criterion) {
let data = create_scrambled_data();
c.bench_function("with_setup", move |b| {
// This will avoid timing the to_vec call.
b.iter_batched(|| data.clone(), |mut data| sort(&mut data), BatchSize::SmallInput)
});
}
criterion_group!(benches, bench);
criterion_main!(benches);
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Bencher
impl RefUnwindSafe for Bencher
impl Send for Bencher
impl Sync for Bencher
impl Unpin for Bencher
impl UnwindSafe for Bencher
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)