use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion}; use rand::prelude::*; use s_tree::STree; pub fn criterion_benchmark(c: &mut Criterion) { println!("Generating seed data..."); let elements: Vec = (0..25_000_000).map(|_| rand::random()).collect(); let mut tree = STree::::new(); for element in &elements { tree.insert(*element, 0); } let mut rng = rand::thread_rng(); for bits in vec![8u32, 16, 24, 32] { for (random_element, existing) in vec![ (*elements.choose(&mut rng).unwrap(), "existing"), (rng.gen(), "non-existing"), ] { { let id = BenchmarkId::new( format!("find_best {}", existing), format!("{} common bits", bits), ); c.bench_with_input(id, &(bits, random_element), { |b, &(bits, random_element)| { b.iter(|| black_box(tree.find_best_single(random_element, bits))) } }); } { let id = BenchmarkId::new( format!("find_best_grouped {}", existing), format!("{} common bits", bits), ); c.bench_with_input(id, &(bits, random_element), { |b, &(bits, random_element)| { b.iter(|| black_box(tree.find_best_sorted(random_element, bits))) } }); } } } } criterion_group!(benches, criterion_benchmark); criterion_main!(benches);