From 0d0338274829e22a44ed18335f5afe72e94b743b Mon Sep 17 00:00:00 2001 From: Zhen Liu Date: Wed, 6 Nov 2024 22:08:48 +0800 Subject: [PATCH 1/3] use critrion instead of feature test, since it is unstablized --- Cargo.toml | 7 +- benches/bench_critrion.rs | 154 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 160 insertions(+), 1 deletion(-) create mode 100644 benches/bench_critrion.rs diff --git a/Cargo.toml b/Cargo.toml index 2e2064a..4073a3c 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_critrion" +harness = false + [dependencies.serde] version = "1.0" optional = true @@ -25,4 +30,4 @@ version = "1.0" optional = true [features] -serialize = ["serde", "serde_derive"] +serialize = ["serde", "serde_derive"] \ No newline at end of file diff --git a/benches/bench_critrion.rs b/benches/bench_critrion.rs new file mode 100644 index 0000000..2dfdd27 --- /dev/null +++ b/benches/bench_critrion.rs @@ -0,0 +1,154 @@ +extern crate kdtree; +extern crate rand; + +extern crate criterion; + +use criterion::{criterion_group, criterion_main, Criterion}; +use kdtree::distance::squared_euclidean; +use kdtree::KdTree; + +fn rand_data() -> ([f64; 3], f64) { + rand::random() +} + +fn bench_add_to_kdtree_with_1k_3d_points(c: &mut Criterion) { + let len = 1000usize; + let point = rand_data(); + let mut points = vec![]; + let mut kdtree = KdTree::with_capacity(3, 16); + for _ in 0..len { + points.push(rand_data()); + } + for i in 0..points.len() { + kdtree.add(&points[i].0, points[i].1).unwrap(); + } + c.bench_function("bench_add_to_kdtree_with_1k_3d_points", |b| { + b.iter(|| kdtree.add(&point.0, point.1).unwrap()); + }); +} +fn bench_nearest_from_kdtree_with_1k_3d_points(c: &mut Criterion) { + let len = 1000usize; + let point = rand_data(); + let mut points = vec![]; + let mut kdtree = KdTree::with_capacity(3, 16); + for _ in 0..len { + points.push(rand_data()); + } + for i in 0..points.len() { + kdtree.add(&points[i].0, points[i].1).unwrap(); + } + c.bench_function("bench_nearest_from_kdtree_with_1k_3d_points", |b| { + b.iter(|| kdtree.nearest(&point.0, 8, &squared_euclidean).unwrap()); + }); +} + +fn bench_within_2k_data_01_radius(c: &mut Criterion) { + let len = 2000usize; + let point = rand_data(); + let mut points = vec![]; + let mut kdtree = KdTree::with_capacity(3, 16); + for _ in 0..len { + points.push(rand_data()); + } + for i in 0..points.len() { + kdtree.add(&points[i].0, points[i].1).unwrap(); + } + + c.bench_function("bench_within_2k_data_01_radius", |b| { + b.iter(|| kdtree.within(&point.0, 0.1, &squared_euclidean).unwrap()); + }); +} + +fn bench_within_2k_data_02_radius(c: &mut Criterion) { + let len = 2000usize; + let point = rand_data(); + let mut points = vec![]; + let mut kdtree = KdTree::with_capacity(3, 16); + for _ in 0..len { + points.push(rand_data()); + } + for i in 0..points.len() { + kdtree.add(&points[i].0, points[i].1).unwrap(); + } + c.bench_function("bench_within_2k_data_02_radius", |b| { + b.iter(|| kdtree.within(&point.0, 0.2, &squared_euclidean).unwrap()); + }); +} + +fn bench_within_unsorted_2k_data_01_radius(c: &mut Criterion) { + let len = 2000usize; + let point = rand_data(); + let mut points = vec![]; + let mut kdtree = KdTree::with_capacity(3, 16); + for _ in 0..len { + points.push(rand_data()); + } + for i in 0..points.len() { + kdtree.add(&points[i].0, points[i].1).unwrap(); + } + c.bench_function("bench_within_unsorted_2k_data_01_radius", |b| { + b.iter(|| kdtree.within_unsorted(&point.0, 0.1, &squared_euclidean).unwrap()); + }); +} + +fn bench_within_unsorted_2k_data_02_radius(c: &mut Criterion) { + let len = 2000usize; + let point = rand_data(); + let mut points = vec![]; + let mut kdtree = KdTree::with_capacity(3, 16); + for _ in 0..len { + points.push(rand_data()); + } + for i in 0..points.len() { + kdtree.add(&points[i].0, points[i].1).unwrap(); + } + c.bench_function("bench_within_unsorted_2k_data_02_radius", |b| { + b.iter(|| kdtree.within_unsorted(&point.0, 0.2, &squared_euclidean).unwrap()); + }); +} + +fn bench_within_count_2k_data_01_radius(c: &mut Criterion) { + let len = 2000usize; + let point = rand_data(); + let mut points = vec![]; + let mut kdtree = KdTree::with_capacity(3, 16); + for _ in 0..len { + points.push(rand_data()); + } + for i in 0..points.len() { + kdtree.add(&points[i].0, points[i].1).unwrap(); + } + c.bench_function("bench_within_count_2k_data_01_radius", |b| { + b.iter(|| kdtree.within_count(&point.0, 0.1, &squared_euclidean).unwrap()); + }); +} + +fn bench_within_count_2k_data_02_radius(c: &mut Criterion) { + let len = 2000usize; + let point = rand_data(); + let mut points = vec![]; + let mut kdtree = KdTree::with_capacity(3, 16); + for _ in 0..len { + points.push(rand_data()); + } + for i in 0..points.len() { + kdtree.add(&points[i].0, points[i].1).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); From 6789aaa5fbe02e2de151cb395c0d30774c57c93e Mon Sep 17 00:00:00 2001 From: Rui Hu Date: Fri, 29 Nov 2024 17:59:28 -0800 Subject: [PATCH 2/3] fixes --- Cargo.toml | 5 +- benches/bench.rs | 81 ++++++++++++-------- benches/bench_critrion.rs | 154 -------------------------------------- 3 files changed, 52 insertions(+), 188 deletions(-) delete mode 100644 benches/bench_critrion.rs diff --git a/Cargo.toml b/Cargo.toml index 4073a3c..44fe7a2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,7 +18,7 @@ num-traits = "0.2" thiserror = "1.0" [[bench]] -name = "bench_critrion" +name = "bench" harness = false [dependencies.serde] @@ -30,4 +30,5 @@ version = "1.0" optional = true [features] -serialize = ["serde", "serde_derive"] \ No newline at end of file +serialize = ["serde", "serde_derive"] + diff --git a/benches/bench.rs b/benches/bench.rs index aecdaf3..e26514f 100644 --- a/benches/bench.rs +++ b/benches/bench.rs @@ -1,18 +1,17 @@ -#![feature(test)] extern crate kdtree; extern crate rand; -extern crate test; +extern crate criterion; + +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![]; @@ -23,11 +22,12 @@ fn bench_add_to_kdtree_with_1k_3d_points(b: &mut Bencher) { for i in 0..points.len() { kdtree.add(&points[i].0, points[i].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![]; @@ -38,11 +38,12 @@ fn bench_nearest_from_kdtree_with_1k_3d_points(b: &mut Bencher) { for i in 0..points.len() { kdtree.add(&points[i].0, points[i].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![]; @@ -54,11 +55,12 @@ fn bench_within_2k_data_01_radius(b: &mut Bencher) { kdtree.add(&points[i].0, points[i].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![]; @@ -69,12 +71,12 @@ fn bench_within_2k_data_02_radius(b: &mut Bencher) { for i in 0..points.len() { kdtree.add(&points[i].0, points[i].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![]; @@ -85,12 +87,12 @@ fn bench_within_unsorted_2k_data_01_radius(b: &mut Bencher) { for i in 0..points.len() { kdtree.add(&points[i].0, points[i].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![]; @@ -101,12 +103,12 @@ fn bench_within_unsorted_2k_data_02_radius(b: &mut Bencher) { for i in 0..points.len() { kdtree.add(&points[i].0, points[i].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![]; @@ -117,12 +119,12 @@ fn bench_within_count_2k_data_01_radius(b: &mut Bencher) { for i in 0..points.len() { kdtree.add(&points[i].0, points[i].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![]; @@ -133,6 +135,21 @@ fn bench_within_count_2k_data_02_radius(b: &mut Bencher) { for i in 0..points.len() { kdtree.add(&points[i].0, points[i].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); diff --git a/benches/bench_critrion.rs b/benches/bench_critrion.rs deleted file mode 100644 index 2dfdd27..0000000 --- a/benches/bench_critrion.rs +++ /dev/null @@ -1,154 +0,0 @@ -extern crate kdtree; -extern crate rand; - -extern crate criterion; - -use criterion::{criterion_group, criterion_main, Criterion}; -use kdtree::distance::squared_euclidean; -use kdtree::KdTree; - -fn rand_data() -> ([f64; 3], f64) { - rand::random() -} - -fn bench_add_to_kdtree_with_1k_3d_points(c: &mut Criterion) { - let len = 1000usize; - let point = rand_data(); - let mut points = vec![]; - let mut kdtree = KdTree::with_capacity(3, 16); - for _ in 0..len { - points.push(rand_data()); - } - for i in 0..points.len() { - kdtree.add(&points[i].0, points[i].1).unwrap(); - } - c.bench_function("bench_add_to_kdtree_with_1k_3d_points", |b| { - b.iter(|| kdtree.add(&point.0, point.1).unwrap()); - }); -} -fn bench_nearest_from_kdtree_with_1k_3d_points(c: &mut Criterion) { - let len = 1000usize; - let point = rand_data(); - let mut points = vec![]; - let mut kdtree = KdTree::with_capacity(3, 16); - for _ in 0..len { - points.push(rand_data()); - } - for i in 0..points.len() { - kdtree.add(&points[i].0, points[i].1).unwrap(); - } - c.bench_function("bench_nearest_from_kdtree_with_1k_3d_points", |b| { - b.iter(|| kdtree.nearest(&point.0, 8, &squared_euclidean).unwrap()); - }); -} - -fn bench_within_2k_data_01_radius(c: &mut Criterion) { - let len = 2000usize; - let point = rand_data(); - let mut points = vec![]; - let mut kdtree = KdTree::with_capacity(3, 16); - for _ in 0..len { - points.push(rand_data()); - } - for i in 0..points.len() { - kdtree.add(&points[i].0, points[i].1).unwrap(); - } - - c.bench_function("bench_within_2k_data_01_radius", |b| { - b.iter(|| kdtree.within(&point.0, 0.1, &squared_euclidean).unwrap()); - }); -} - -fn bench_within_2k_data_02_radius(c: &mut Criterion) { - let len = 2000usize; - let point = rand_data(); - let mut points = vec![]; - let mut kdtree = KdTree::with_capacity(3, 16); - for _ in 0..len { - points.push(rand_data()); - } - for i in 0..points.len() { - kdtree.add(&points[i].0, points[i].1).unwrap(); - } - c.bench_function("bench_within_2k_data_02_radius", |b| { - b.iter(|| kdtree.within(&point.0, 0.2, &squared_euclidean).unwrap()); - }); -} - -fn bench_within_unsorted_2k_data_01_radius(c: &mut Criterion) { - let len = 2000usize; - let point = rand_data(); - let mut points = vec![]; - let mut kdtree = KdTree::with_capacity(3, 16); - for _ in 0..len { - points.push(rand_data()); - } - for i in 0..points.len() { - kdtree.add(&points[i].0, points[i].1).unwrap(); - } - c.bench_function("bench_within_unsorted_2k_data_01_radius", |b| { - b.iter(|| kdtree.within_unsorted(&point.0, 0.1, &squared_euclidean).unwrap()); - }); -} - -fn bench_within_unsorted_2k_data_02_radius(c: &mut Criterion) { - let len = 2000usize; - let point = rand_data(); - let mut points = vec![]; - let mut kdtree = KdTree::with_capacity(3, 16); - for _ in 0..len { - points.push(rand_data()); - } - for i in 0..points.len() { - kdtree.add(&points[i].0, points[i].1).unwrap(); - } - c.bench_function("bench_within_unsorted_2k_data_02_radius", |b| { - b.iter(|| kdtree.within_unsorted(&point.0, 0.2, &squared_euclidean).unwrap()); - }); -} - -fn bench_within_count_2k_data_01_radius(c: &mut Criterion) { - let len = 2000usize; - let point = rand_data(); - let mut points = vec![]; - let mut kdtree = KdTree::with_capacity(3, 16); - for _ in 0..len { - points.push(rand_data()); - } - for i in 0..points.len() { - kdtree.add(&points[i].0, points[i].1).unwrap(); - } - c.bench_function("bench_within_count_2k_data_01_radius", |b| { - b.iter(|| kdtree.within_count(&point.0, 0.1, &squared_euclidean).unwrap()); - }); -} - -fn bench_within_count_2k_data_02_radius(c: &mut Criterion) { - let len = 2000usize; - let point = rand_data(); - let mut points = vec![]; - let mut kdtree = KdTree::with_capacity(3, 16); - for _ in 0..len { - points.push(rand_data()); - } - for i in 0..points.len() { - kdtree.add(&points[i].0, points[i].1).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); From e5a549496a7600de18f6874ec4e3b831e121d176 Mon Sep 17 00:00:00 2001 From: Rui Hu Date: Fri, 29 Nov 2024 18:23:47 -0800 Subject: [PATCH 3/3] warnings --- benches/bench.rs | 36 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/benches/bench.rs b/benches/bench.rs index e26514f..b495c1a 100644 --- a/benches/bench.rs +++ b/benches/bench.rs @@ -1,8 +1,7 @@ +extern crate criterion; extern crate kdtree; extern crate rand; -extern crate criterion; - use criterion::{criterion_group, criterion_main, Criterion}; use kdtree::distance::squared_euclidean; use kdtree::KdTree; @@ -19,8 +18,8 @@ fn bench_add_to_kdtree_with_1k_3d_points(c: &mut Criterion) { 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(); } c.bench_function("bench_add_to_kdtree_with_1k_3d_points", |b| { b.iter(|| kdtree.add(&point.0, point.1).unwrap()); @@ -35,8 +34,8 @@ fn bench_nearest_from_kdtree_with_1k_3d_points(c: &mut Criterion) { 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(); } c.bench_function("bench_nearest_from_kdtree_with_1k_3d_points", |b| { b.iter(|| kdtree.nearest(&point.0, 8, &squared_euclidean).unwrap()); @@ -51,10 +50,9 @@ fn bench_within_2k_data_01_radius(c: &mut Criterion) { 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(); } - c.bench_function("bench_within_2k_data_01_radius", |b| { b.iter(|| kdtree.within(&point.0, 0.1, &squared_euclidean).unwrap()); }); @@ -68,8 +66,8 @@ fn bench_within_2k_data_02_radius(c: &mut Criterion) { 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(); } c.bench_function("bench_within_2k_data_02_radius", |b| { b.iter(|| kdtree.within(&point.0, 0.2, &squared_euclidean).unwrap()); @@ -84,8 +82,8 @@ fn bench_within_unsorted_2k_data_01_radius(c: &mut Criterion) { 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(); } c.bench_function("bench_within_unsorted_2k_data_01_radius", |b| { b.iter(|| kdtree.within_unsorted(&point.0, 0.1, &squared_euclidean).unwrap()); @@ -100,8 +98,8 @@ fn bench_within_unsorted_2k_data_02_radius(c: &mut Criterion) { 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(); } c.bench_function("bench_within_unsorted_2k_data_02_radius", |b| { b.iter(|| kdtree.within_unsorted(&point.0, 0.2, &squared_euclidean).unwrap()); @@ -116,8 +114,8 @@ fn bench_within_count_2k_data_01_radius(c: &mut Criterion) { 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(); } c.bench_function("bench_within_count_2k_data_01_radius", |b| { b.iter(|| kdtree.within_count(&point.0, 0.1, &squared_euclidean).unwrap()); @@ -132,8 +130,8 @@ fn bench_within_count_2k_data_02_radius(c: &mut Criterion) { 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(); } c.bench_function("bench_within_count_2k_data_02_radius", |b| { b.iter(|| kdtree.within_count(&point.0, 0.2, &squared_euclidean).unwrap());