2024-03-15 19:15:21 +00:00
|
|
|
use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion};
|
2022-09-20 17:02:56 +00:00
|
|
|
use rand::Rng;
|
|
|
|
|
2024-03-15 19:15:21 +00:00
|
|
|
static ITERS: u32 = 10_000;
|
|
|
|
|
2022-09-20 17:02:56 +00:00
|
|
|
fn compare_crates(c: &mut Criterion) {
|
|
|
|
let mut rng = rand::thread_rng();
|
|
|
|
let mut group = c.benchmark_group("Comparison");
|
2024-03-15 19:15:21 +00:00
|
|
|
for redundancy in [1, 5, 10, 20, 40] {
|
|
|
|
let samples: Vec<u32> = (0..ITERS)
|
|
|
|
.map(|_| rng.gen_range(0..ITERS / redundancy))
|
|
|
|
.collect();
|
2022-09-20 17:02:56 +00:00
|
|
|
group.bench_with_input(
|
2024-03-15 19:15:21 +00:00
|
|
|
BenchmarkId::new("median_accumulator", redundancy),
|
2022-09-20 17:02:56 +00:00
|
|
|
&samples,
|
|
|
|
|b, _i| {
|
|
|
|
b.iter(|| {
|
2023-04-14 10:39:11 +00:00
|
|
|
let mut median = median_accumulator::vec::MedianAcc::new();
|
2022-09-20 17:02:56 +00:00
|
|
|
samples.iter().for_each(|s| median.push(*s));
|
2024-03-15 19:15:21 +00:00
|
|
|
black_box(median.get_median());
|
2022-09-20 17:02:56 +00:00
|
|
|
})
|
|
|
|
},
|
|
|
|
);
|
|
|
|
group.bench_with_input(
|
2024-03-15 19:15:21 +00:00
|
|
|
BenchmarkId::new("medianheap", redundancy),
|
2022-09-20 17:02:56 +00:00
|
|
|
&samples,
|
|
|
|
|b, _i| {
|
|
|
|
b.iter(|| {
|
|
|
|
let mut median = medianheap::MedianHeap::new();
|
|
|
|
samples.iter().for_each(|s| median.push(*s));
|
2024-03-15 19:15:21 +00:00
|
|
|
black_box(median.median());
|
2022-09-20 17:02:56 +00:00
|
|
|
})
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
group.finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
criterion_group!(benches, compare_crates);
|
|
|
|
criterion_main!(benches);
|