use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion}; use iterstats::Iterstats; use rand::seq::IteratorRandom; use std::hint::black_box; fn naive<'a>(iter: impl Iterator + Clone) -> Option<(usize, usize)> { iter.clone() .min() .and_then(|min| iter.max().and_then(|max| Some((*min, *max)))) } fn bench_range(c: &mut Criterion) { let mut group = c.benchmark_group("range"); let mut rng = &mut rand::thread_rng(); for len in [10, 100, 1000, 10000, 100_000, 1_000_000] { let data = (0..2 * len).choose_multiple(&mut rng, len); assert_eq!(data.len(), len); group.bench_with_input(BenchmarkId::new("naive", len), &data, |b, i| { b.iter(|| black_box(naive(black_box(i.iter())))) }); group.bench_with_input(BenchmarkId::new("iterstats", len), &data, |b, i| { b.iter(|| black_box(black_box(i.iter()).range())) }); } group.finish(); } criterion_group!(benches, bench_range); criterion_main!(benches);