Skip to content

Commit

Permalink
use critrion instead of feature test, since it is unstablized
Browse files Browse the repository at this point in the history
  • Loading branch information
HernandoR committed Nov 6, 2024
1 parent 4f3755e commit e1f8e5e
Show file tree
Hide file tree
Showing 2 changed files with 160 additions and 1 deletion.
7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -25,4 +30,4 @@ version = "1.0"
optional = true

[features]
serialize = ["serde", "serde_derive"]
serialize = ["serde", "serde_derive"]
154 changes: 154 additions & 0 deletions benches/bench_critrion.rs
Original file line number Diff line number Diff line change
@@ -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);

0 comments on commit e1f8e5e

Please sign in to comment.