diff --git a/Cargo.toml b/Cargo.toml index 2e2064a..44fe7a2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,9 +13,14 @@ license = "MIT OR Apache-2.0" rand = "0.8.5" [dependencies] +criterion = "0.5.1" num-traits = "0.2" thiserror = "1.0" +[[bench]] +name = "bench" +harness = false + [dependencies.serde] version = "1.0" optional = true @@ -26,3 +31,4 @@ optional = true [features] serialize = ["serde", "serde_derive"] + diff --git a/benches/bench.rs b/benches/bench.rs index aecdaf3..b495c1a 100644 --- a/benches/bench.rs +++ b/benches/bench.rs @@ -1,18 +1,16 @@ -#![feature(test)] +extern crate criterion; extern crate kdtree; extern crate rand; -extern crate test; +use criterion::{criterion_group, criterion_main, Criterion}; use kdtree::distance::squared_euclidean; use kdtree::KdTree; -use test::Bencher; fn rand_data() -> ([f64; 3], f64) { rand::random() } -#[bench] -fn bench_add_to_kdtree_with_1k_3d_points(b: &mut Bencher) { +fn bench_add_to_kdtree_with_1k_3d_points(c: &mut Criterion) { let len = 1000usize; let point = rand_data(); let mut points = vec![]; @@ -20,14 +18,15 @@ fn bench_add_to_kdtree_with_1k_3d_points(b: &mut Bencher) { for _ in 0..len { points.push(rand_data()); } - for i in 0..points.len() { - kdtree.add(&points[i].0, points[i].1).unwrap(); + for point in points.iter() { + kdtree.add(&point.0, point.1).unwrap(); } - b.iter(|| kdtree.add(&point.0, point.1).unwrap()); + c.bench_function("bench_add_to_kdtree_with_1k_3d_points", |b| { + b.iter(|| kdtree.add(&point.0, point.1).unwrap()); + }); } -#[bench] -fn bench_nearest_from_kdtree_with_1k_3d_points(b: &mut Bencher) { +fn bench_nearest_from_kdtree_with_1k_3d_points(c: &mut Criterion) { let len = 1000usize; let point = rand_data(); let mut points = vec![]; @@ -35,14 +34,15 @@ fn bench_nearest_from_kdtree_with_1k_3d_points(b: &mut Bencher) { for _ in 0..len { points.push(rand_data()); } - for i in 0..points.len() { - kdtree.add(&points[i].0, points[i].1).unwrap(); + for point in points.iter() { + kdtree.add(&point.0, point.1).unwrap(); } - b.iter(|| kdtree.nearest(&point.0, 8, &squared_euclidean).unwrap()); + c.bench_function("bench_nearest_from_kdtree_with_1k_3d_points", |b| { + b.iter(|| kdtree.nearest(&point.0, 8, &squared_euclidean).unwrap()); + }); } -#[bench] -fn bench_within_2k_data_01_radius(b: &mut Bencher) { +fn bench_within_2k_data_01_radius(c: &mut Criterion) { let len = 2000usize; let point = rand_data(); let mut points = vec![]; @@ -50,15 +50,15 @@ fn bench_within_2k_data_01_radius(b: &mut Bencher) { for _ in 0..len { points.push(rand_data()); } - for i in 0..points.len() { - kdtree.add(&points[i].0, points[i].1).unwrap(); + for point in points.iter() { + kdtree.add(&point.0, point.1).unwrap(); } - - b.iter(|| kdtree.within(&point.0, 0.1, &squared_euclidean).unwrap()); + c.bench_function("bench_within_2k_data_01_radius", |b| { + b.iter(|| kdtree.within(&point.0, 0.1, &squared_euclidean).unwrap()); + }); } -#[bench] -fn bench_within_2k_data_02_radius(b: &mut Bencher) { +fn bench_within_2k_data_02_radius(c: &mut Criterion) { let len = 2000usize; let point = rand_data(); let mut points = vec![]; @@ -66,15 +66,15 @@ fn bench_within_2k_data_02_radius(b: &mut Bencher) { for _ in 0..len { points.push(rand_data()); } - for i in 0..points.len() { - kdtree.add(&points[i].0, points[i].1).unwrap(); + for point in points.iter() { + kdtree.add(&point.0, point.1).unwrap(); } - - b.iter(|| kdtree.within(&point.0, 0.2, &squared_euclidean).unwrap()); + c.bench_function("bench_within_2k_data_02_radius", |b| { + b.iter(|| kdtree.within(&point.0, 0.2, &squared_euclidean).unwrap()); + }); } -#[bench] -fn bench_within_unsorted_2k_data_01_radius(b: &mut Bencher) { +fn bench_within_unsorted_2k_data_01_radius(c: &mut Criterion) { let len = 2000usize; let point = rand_data(); let mut points = vec![]; @@ -82,15 +82,15 @@ fn bench_within_unsorted_2k_data_01_radius(b: &mut Bencher) { for _ in 0..len { points.push(rand_data()); } - for i in 0..points.len() { - kdtree.add(&points[i].0, points[i].1).unwrap(); + for point in points.iter() { + kdtree.add(&point.0, point.1).unwrap(); } - - b.iter(|| kdtree.within_unsorted(&point.0, 0.1, &squared_euclidean).unwrap()); + c.bench_function("bench_within_unsorted_2k_data_01_radius", |b| { + b.iter(|| kdtree.within_unsorted(&point.0, 0.1, &squared_euclidean).unwrap()); + }); } -#[bench] -fn bench_within_unsorted_2k_data_02_radius(b: &mut Bencher) { +fn bench_within_unsorted_2k_data_02_radius(c: &mut Criterion) { let len = 2000usize; let point = rand_data(); let mut points = vec![]; @@ -98,15 +98,15 @@ fn bench_within_unsorted_2k_data_02_radius(b: &mut Bencher) { for _ in 0..len { points.push(rand_data()); } - for i in 0..points.len() { - kdtree.add(&points[i].0, points[i].1).unwrap(); + for point in points.iter() { + kdtree.add(&point.0, point.1).unwrap(); } - - b.iter(|| kdtree.within_unsorted(&point.0, 0.2, &squared_euclidean).unwrap()); + c.bench_function("bench_within_unsorted_2k_data_02_radius", |b| { + b.iter(|| kdtree.within_unsorted(&point.0, 0.2, &squared_euclidean).unwrap()); + }); } -#[bench] -fn bench_within_count_2k_data_01_radius(b: &mut Bencher) { +fn bench_within_count_2k_data_01_radius(c: &mut Criterion) { let len = 2000usize; let point = rand_data(); let mut points = vec![]; @@ -114,15 +114,15 @@ fn bench_within_count_2k_data_01_radius(b: &mut Bencher) { for _ in 0..len { points.push(rand_data()); } - for i in 0..points.len() { - kdtree.add(&points[i].0, points[i].1).unwrap(); + for point in points.iter() { + kdtree.add(&point.0, point.1).unwrap(); } - - b.iter(|| kdtree.within_count(&point.0, 0.1, &squared_euclidean).unwrap()); + c.bench_function("bench_within_count_2k_data_01_radius", |b| { + b.iter(|| kdtree.within_count(&point.0, 0.1, &squared_euclidean).unwrap()); + }); } -#[bench] -fn bench_within_count_2k_data_02_radius(b: &mut Bencher) { +fn bench_within_count_2k_data_02_radius(c: &mut Criterion) { let len = 2000usize; let point = rand_data(); let mut points = vec![]; @@ -130,9 +130,24 @@ fn bench_within_count_2k_data_02_radius(b: &mut Bencher) { for _ in 0..len { points.push(rand_data()); } - for i in 0..points.len() { - kdtree.add(&points[i].0, points[i].1).unwrap(); + for point in points.iter() { + kdtree.add(&point.0, point.1).unwrap(); } - - b.iter(|| kdtree.within_count(&point.0, 0.2, &squared_euclidean).unwrap()); + c.bench_function("bench_within_count_2k_data_02_radius", |b| { + b.iter(|| kdtree.within_count(&point.0, 0.2, &squared_euclidean).unwrap()); + }); } + +criterion_group!( + benches, + bench_add_to_kdtree_with_1k_3d_points, + bench_nearest_from_kdtree_with_1k_3d_points, + bench_within_2k_data_01_radius, + bench_within_2k_data_02_radius, + bench_within_unsorted_2k_data_01_radius, + bench_within_unsorted_2k_data_02_radius, + bench_within_count_2k_data_01_radius, + bench_within_count_2k_data_02_radius, +); + +criterion_main!(benches);