Struct pew::Benchmark
[−]
[src]
pub struct Benchmark<T: 'static + Clone> { /* fields omitted */ }
The main Benchmark struct
A benchmark consists of the following:
name: &'static str
- will be prefixed to the output (seerun
)- One or more
bench: fn(&mut State<T>)
which are the actual benchmark. This is the actual benchmark which is timed - A range which consists of a
lower_bound
, anupper_bound
, and amul
factor. Will run the benchmark once for eachi in [lower_bound, upperbound]
such thati
is initialized tolower_bound
and gets multiplied bymul
. Defaults to:lower_bound = 1
upper_bound = 1 << 20
mul = 2
generator: fn(u64) -> T
(optional) - rather than passingi
from above as input to the benchmark, T produced by this method is passed in instead. For eachi
,gen(i)
is called once for eachbench
in thisbenchmark
. The result of this is then cloned and passed intobench
each time it is run.
Examples
#[macro_use] extern crate pew; use pew::Benchmark; fn get_vec(n: u64) -> Vec<u64> { let mut vec = Vec::new(); for i in 0..n { vec.push(i as u64); } return vec; } fn bm_vector_gen(state: &mut pew::State<Vec<u64>>) { let mut vec = state.get_input(); let n = vec.len() as u64; for _ in 0..n { pew::do_not_optimize(vec.pop()); } } fn main() { Benchmark::with_name("range_bench") .with_range(1 << 5, 1 << 5, 2) .with_generator(get_vec) .with_bench(pew_bench!(bm_vector_gen)) .run(); }
This will output:
Name,Time (ns)
gen_bench/f/1024,103674
gen_bench/f/4096,412499
gen_bench/f/16384,1634809
gen_bench/f/65536,7168879
gen_bench/f/262144,27419824
gen_bench/f/1048576,109010591
When running the benchmark, you can pass the --filter
flag. This will only run benchmarks
who's name contains the substring passed in to --filter
.
See examples/
for more examples.
Methods
impl Benchmark<u64>
[src]
impl<T: Clone> Benchmark<T>
[src]
pub fn with_lower_bound(self, lb: u64) -> Self
[src]
Sets the lower_bound
for this benchmark
pub fn with_upper_bound(self, ub: u64) -> Self
[src]
Sets the upper_bound
for this benchmark
pub fn with_mul(self, mul: u64) -> Self
[src]
Sets the mul
for this benchmark
pub fn with_range(self, lb: u64, ub: u64, mul: u64) -> Self
[src]
Sets the entire range for this benchmark
pub fn with_generator<U: Clone>(self, gen: fn(_: T) -> U) -> Benchmark<U>
[src]
Sets a generator for this benchmark
Multiple generators can be specified, each of which will be fn(T) -> U
. These will be
composed with the previous generators.
Note that calling this function will wipe out all previously set bench
es. Therefore, this
function should be called before calling with_bench
.
pub fn with_bench(self, t: (&'static str, fn(_: &mut State<T>))) -> Self
[src]
Specifies a benchmark method
This must be called one or more times before calling run
. All functions in this suite
will have the same range and generator(s).
This accepts a tuple with the benchmark name and the function. If the function is an ident
and you want the benchmark name to match the function, use pew_bench!
.
pub fn run(self)
[src]
Runs the benchmark
Prints the result as a csv with the following format:
- Header which will be exactly
Name,Time(ns)
(this will be printed once for the whole program, not once per call to run). - Rows where
name
will be a slash separated concatenation of the benchmark name, the function name, and itime
will be the time in nanoseconds for running the benchmark
Panics
Panics if no bench methods are specified.