Function tiny_bench::run_timed_from_iterator

source ·
pub fn run_timed_from_iterator<T, R, F: FnMut(R) -> T, It>(
    iterator: It,
    closure: F,
) -> TimingData
where It: Iterator<Item = R>,
Expand description

Drains an iterator and calls the closure with the yielded value, timing the closure’s execution.

use std::time::Duration;
use tiny_bench::run_timed_from_iterator;
let it = (0..100);
let mut v = Vec::with_capacity(100);
let mut counted_iterations = 0;
let data = run_timed_from_iterator(it, |i| {
    v.push(i);
    counted_iterations += 1;
});
assert_eq!(100, v.len());
assert_eq!(100, counted_iterations);
data.pretty_print();
Examples found in repository?
examples/time_loop.rs (lines 6-8)
3
4
5
6
7
8
9
10
11
fn main() {
    let generator = 0..100;
    let mut spooky_calculation = 0;
    let results = run_timed_from_iterator(generator, |i| {
        spooky_calculation += i;
    });
    results.pretty_print();
    assert_eq!(4950, spooky_calculation);
}