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.
- If your routine returns a value with an expensive
drop
method, use
iter_with_large_drop
.
- If your routine requires some per-iteration setup that shouldn't be timed,
use
iter_with_setup
or (if the setup is expensive) use iter_with_large_setup
to construct a pool of input data ahead of time
- Otherwise, use
iter
.
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.
let start = Instant::now();
for _ in 0..iters {
routine();
}
let elapsed = start.elapsed();
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)
#[macro_use] extern crate criterion;
use criterion::*;
fn foo() {
}
fn bench(c: &mut Criterion) {
c.bench_function("iter", move |b| {
b.iter(|| foo())
});
}
criterion_group!(benches, bench);
criterion_main!(benches);
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.
#[macro_use] extern crate criterion;
use criterion::*;
fn create_scrambled_data() -> Vec<u64> {
}
fn sort(data: &mut [u64]) {
}
fn bench(c: &mut Criterion) {
let data = create_scrambled_data();
c.bench_function("with_setup", move |b| {
b.iter_with_setup(|| data.to_vec(), |mut data| sort(&mut data))
});
}
criterion_group!(benches, bench);
criterion_main!(benches);
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;
}
elapsed = iters * (Instant::now + routine)
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.
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);
elapsed = Instant::now + iters * (routine) + Iterator::collect::<Vec<_>>
#[macro_use] extern crate criterion;
use criterion::*;
fn create_vector() -> Vec<u64> {
}
fn bench(c: &mut Criterion) {
c.bench_function("with_drop", move |b| {
b.iter_with_large_drop(|| create_vector())
});
}
criterion_group!(benches, bench);
criterion_main!(benches);
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.
let inputs: Vec<()> = (0..iters).map(|_| setup()).collect();
let start = Instant::now();
for input in inputs {
routine(input);
}
let elapsed = start.elapsed();
elapsed = iters * (Instant::now + routine)
#[macro_use] extern crate criterion;
use criterion::*;
fn create_data() -> Vec<u64> {
}
fn use_data(data: &mut [u64]) {
}
fn bench(c: &mut Criterion) {
c.bench_function("with_setup", move |b| {
b.iter_with_large_setup(|| create_data(), |mut data| use_data(&mut data))
});
}
criterion_group!(benches, bench);
criterion_main!(benches);
Performs copy-assignment from source
. Read more
Creates owned data from borrowed data, usually by cloning. Read more
🔬 This is a nightly-only experimental API. (toowned_clone_into
)
recently added
Uses borrowed data to replace owned data, usually by cloning. Read more
🔬 This is a nightly-only experimental API. (try_from
)
The type returned in the event of a conversion error.
🔬 This is a nightly-only experimental API. (try_from
)
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
🔬 This is a nightly-only experimental API. (try_from
)
The type returned in the event of a conversion error.
🔬 This is a nightly-only experimental API. (try_from
)
🔬 This is a nightly-only experimental API. (get_type_id
)
this method will likely be replaced by an associated static