use rand::prelude::thread_rng;
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion, Throughput};
use rand::prelude::*; use std::hint::black_box; use rand_xoshiro::Xoshiro256PlusPlus;
use zima::*;
use rand::SeedableRng;
const SAMPLE_SIZE: usize = 1_000; const RESAMPLES: usize = 10_000;
fn xrng() -> impl rand::Rng {
<Xoshiro256PlusPlus as SeedableRng>::seed_from_u64(thread_rng().next_u64())
}
fn bench_variance_compute(c: &mut Criterion) {
let mut group = c.benchmark_group("variance/compute");
group.throughput(Throughput::Elements(1));
for &size in &[100, 1_000, 10_000] {
let data: Vec<f32> = (0..size).map(|i| (i % 100) as f32).collect();
let statistic = Variance::default();
group.bench_with_input(
BenchmarkId::new("default", size),
&data,
|b, data| b.iter(|| black_box(statistic.compute(black_box(data)))),
);
}
group.finish();
}
fn bench_se_jackknife(c: &mut Criterion) {
let sample: Vec<f32> = (0..SAMPLE_SIZE).map(|i| (i % 100) as f32).collect();
c.bench_function("se/jackknife_mean", |b| {
b.iter(|| black_box(SE::jackknife(black_box(&sample))))
});
}
fn bench_your_pattern(c: &mut Criterion) {
let sample: Sample<f32> = (0..SAMPLE_SIZE).map(|i| (i % 100) as f32).collect();
let statistic = Variance { ddof: 1 };
c.bench_function("pattern/thread_rng", |b| {
b.iter(|| {
let estimated: Sample<f32> = sample::Bootstrap::new(xrng())
.re(&sample)
.take(RESAMPLES)
.map(|res| statistic.compute(&res))
.collect();
black_box(estimated)
})
});
c.bench_function("pattern/xoshiro", |b| {
b.iter(|| {
let estimated: Sample<f32> = sample::Bootstrap::new(xrng())
.re(&sample)
.take(RESAMPLES)
.map(|res| statistic.compute(&res))
.collect();
black_box(estimated)
})
});
}
criterion_group!(
benches,
bench_variance_compute,
bench_se_jackknife,
bench_your_pattern
);
criterion_main!(benches);