Skip to main content

Benchmark

Struct Benchmark 

Source
pub struct Benchmark { /* private fields */ }
Expand description

A single benchmark run.

Collects per-iteration duration samples. Call Benchmark::finish to produce a BenchmarkResult.

§Example

use dev_bench::Benchmark;

let mut b = Benchmark::new("noop");
for _ in 0..10 {
    b.iter(|| std::hint::black_box(42));
}
let r = b.finish();
assert_eq!(r.samples.len(), 10);

Implementations§

Source§

impl Benchmark

Source

pub fn new(name: impl Into<String>) -> Self

Begin a new benchmark with a stable name.

Source

pub fn iter<F, R>(&mut self, f: F) -> R
where F: FnOnce() -> R,

Run one iteration of the benchmark, capturing the duration.

Each call records exactly one sample.

§Example
use dev_bench::Benchmark;

let mut b = Benchmark::new("noop");
b.iter(|| std::hint::black_box(1 + 1));
let r = b.finish();
assert_eq!(r.samples.len(), 1);
Source

pub fn iter_with_count<F>(&mut self, n: u64, f: F)
where F: FnMut(),

Run a closure n times and record ONE sample for the entire batch.

Use for sub-microsecond operations where per-iteration timing would be dominated by Instant::now() overhead. The reported per-iteration mean is batch_duration / n.

§Example
use dev_bench::Benchmark;

let mut b = Benchmark::new("hot");
b.iter_with_count(1000, || {
    std::hint::black_box(40 + 2);
});
let r = b.finish();
assert_eq!(r.samples.len(), 1);
assert_eq!(r.iterations_recorded, 1000);
Source

pub fn finish(self) -> BenchmarkResult

Finalize the benchmark and produce a BenchmarkResult.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.