mirror of
https://github.com/andreytkachenko/kdtree-rust.git
synced 2024-11-22 09:26:25 +04:00
Added bench, improvements in nn code
This commit is contained in:
parent
9ac0a899e2
commit
b9fa192758
39
src/bench.rs
39
src/bench.rs
@ -27,6 +27,27 @@ impl kdtree::kdtree::KdtreePointTrait for Point2WithId {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, PartialEq)]
|
||||
pub struct Point3WithId {
|
||||
dims: [f64; 3],
|
||||
pub id: i32,
|
||||
}
|
||||
|
||||
impl Point3WithId {
|
||||
pub fn new(id: i32, x: f64, y: f64, z: f64) -> Point3WithId {
|
||||
Point3WithId {
|
||||
dims: [x, y, z],
|
||||
id: id,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl kdtree::kdtree::KdtreePointTrait for Point3WithId {
|
||||
fn dims(&self) -> &[f64] {
|
||||
return &self.dims;
|
||||
}
|
||||
}
|
||||
|
||||
fn a(b: &mut Bencher) {
|
||||
let len = 1000usize;
|
||||
let mut points : Vec<Point2WithId> = vec![];
|
||||
@ -42,5 +63,21 @@ fn a(b: &mut Bencher) {
|
||||
});
|
||||
}
|
||||
|
||||
benchmark_group!(benches, a);
|
||||
fn b(b: &mut Bencher) {
|
||||
let len = 1000usize;
|
||||
let mut points : Vec<Point3WithId> = vec![];
|
||||
//let mut kdtree = KdTree::new_with_capacity(3, 16);
|
||||
|
||||
for i in 0..len {
|
||||
points.push(Point3WithId::new(i as i32, rand::random(),rand::random(),rand::random()))
|
||||
|
||||
}
|
||||
|
||||
let tree = kdtree::kdtree::Kdtree::new(points.clone());
|
||||
|
||||
|
||||
b.iter(|| tree.nearest_search(&points[0]));
|
||||
}
|
||||
|
||||
benchmark_group!(benches, a,b);
|
||||
benchmark_main!(benches);
|
@ -47,19 +47,15 @@ impl<T: KdtreePointTrait + Copy> Kdtree<T> {
|
||||
let splitting_value = node.split_on;
|
||||
let point_splitting_dim_value = p.dims()[dimension];
|
||||
|
||||
let mut closer_node : Option<usize>;
|
||||
let mut farther_node : Option<usize>;
|
||||
|
||||
let (closer_node, farther_node) =
|
||||
if point_splitting_dim_value <= splitting_value {
|
||||
closer_node = node.left_node;
|
||||
farther_node = node.right_node;
|
||||
(node.left_node, node.right_node)
|
||||
} else {
|
||||
closer_node = node.right_node;
|
||||
farther_node = node.left_node;
|
||||
}
|
||||
(node.right_node, node.left_node)
|
||||
};
|
||||
|
||||
if closer_node.is_some() {
|
||||
self.nearest_search_impl(p, closer_node.unwrap(), best_distance_squared, best_leaf_found);
|
||||
if let Some(closer_node) = closer_node {
|
||||
self.nearest_search_impl(p, closer_node, best_distance_squared, best_leaf_found);
|
||||
}
|
||||
|
||||
let distance = squared_euclidean(p.dims(), node.point.dims());
|
||||
@ -68,14 +64,13 @@ impl<T: KdtreePointTrait + Copy> Kdtree<T> {
|
||||
*best_leaf_found = searched_index;
|
||||
}
|
||||
|
||||
if(farther_node.is_some()) {
|
||||
if let Some(farther_node) = farther_node {
|
||||
let distance_on_single_dimension = squared_euclidean(&[splitting_value],&[point_splitting_dim_value]);
|
||||
|
||||
if distance_on_single_dimension <= *best_distance_squared {
|
||||
self.nearest_search_impl(p, farther_node.unwrap(), best_distance_squared, best_leaf_found);
|
||||
self.nearest_search_impl(p, farther_node, best_distance_squared, best_leaf_found);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user