diff --git a/benches/benchmark.rs b/benches/benchmark.rs index 992d01f..47c2566 100644 --- a/benches/benchmark.rs +++ b/benches/benchmark.rs @@ -2,37 +2,40 @@ use criterion::{criterion_group, criterion_main, Criterion}; use rand::{rngs::StdRng, Rng, SeedableRng}; use rindex::Index; -fn benchmark(criterion: &mut Criterion) { - let k = 10; - let n = 10000; - let seed = 0; +const K: usize = 10; +const SEED: u64 = 0; +const N: usize = 10000; +fn benchmark(criterion: &mut Criterion) { let mut group = criterion.benchmark_group("rknn"); group.sample_size(10); - group.bench_function("SSTree", |b| b.iter(|| bench_sstree(k, n, seed))); - group.bench_function("Linear", |b| b.iter(|| bench_linear(k, n, seed))); + group.bench_function("SSTree", |b| b.iter(|| bench_sstree())); + group.bench_function("Linear", |b| b.iter(|| bench_linear())); } criterion_group!(benches, benchmark); criterion_main!(benches); -fn bench_sstree(k: usize, n: usize, seed: u64) { - let mut tree = rindex::SSTree::new(k); - let mut rng = StdRng::seed_from_u64(seed); - for _ in 0..n { - let point = [rng.gen(), rng.gen()]; - tree.rknn(point); - tree.insert(point); +fn bench_sstree() { + let mut tree = rindex::SSTree::new(K); + let pts = dataset(); + for p in pts { + tree.rknn(p); + tree.insert(p); } } -fn bench_linear(k: usize, n: usize, seed: u64) { - let mut linear = rindex::LinearIndex::new(k); - let mut rng = StdRng::seed_from_u64(seed); - for _ in 0..n { - let point = [rng.gen(), rng.gen()]; +fn bench_linear() { + let mut linear = rindex::LinearIndex::new(K); + let dataset = dataset(); + for point in dataset { linear.rknn(point); linear.insert(point); } } + +fn dataset() -> Vec<[f64; 2]> { + let mut rng = StdRng::seed_from_u64(SEED); + (0..N).map(|_| [rng.gen(), rng.gen()]).collect() +}