From 58f19cdb9a1d8f8fc9bc640e71ba2eb7ca203044 Mon Sep 17 00:00:00 2001 From: Joshua Coles Date: Thu, 9 Mar 2023 08:27:34 +0000 Subject: [PATCH] Attempt to speed up and add some logging --- src/cli/mod.rs | 13 ++++++++++++- src/system/spaces/kd_grid.rs | 19 +++++++++---------- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/src/cli/mod.rs b/src/cli/mod.rs index 035d765..e3c5c95 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -4,6 +4,7 @@ use crate::system::{Position, Storage}; use crate::system::spawner::Spawner; use crate::system::sticker::Sticker; use crate::system::walker::Walker; +use std::time::SystemTime; pub mod cli; pub mod output; @@ -13,12 +14,22 @@ pub fn drive_system, W: Walker

, Sp: Spawne max_frames: Option, notify_every: Option, ) { + let start = SystemTime::now(); + let mut prev = start.clone(); let mut previous_n: usize = 0; while sys.running { sys.update(); if let Some(notify_every) = notify_every && (sys.history.len() % notify_every) == 0 && previous_n != sys.history.len() { - println!("On frame {}, deposited {} particles", sys.frame, sys.history.len()); + let now = SystemTime::now(); + + println!("[{}ms, d = {}ms] On frame {}, deposited {} particles", + now.duration_since(start).unwrap().as_millis(), + now.duration_since(prev).unwrap().as_millis(), + sys.frame, sys.history.len() + ); + + prev = now; previous_n = sys.history.len(); } diff --git a/src/system/spaces/kd_grid.rs b/src/system/spaces/kd_grid.rs index f483308..8c5ab4d 100644 --- a/src/system/spaces/kd_grid.rs +++ b/src/system/spaces/kd_grid.rs @@ -17,8 +17,10 @@ pub struct KDGrid { impl Storage for KDGrid<2> { fn is_occupied(&self, position: &Grid2D) -> bool { - let (dist_sq, _) = self.inner.nearest_one(&[position.x as f32, position.y as f32], &taxicab_grid2).unwrap(); - dist_sq == 0f32 + let a = self.inner.best_n_within(&[position.x as f32, position.y as f32], 0f32, 1, &taxicab_grid2).unwrap(); + !a.is_empty() + // let (dist_sq, _) = self.inner.nearest_one(&[position.x as f32, position.y as f32], &taxicab_grid2).unwrap(); + // dist_sq == 0f32 } fn deposit(&mut self, position: &Grid2D) { @@ -29,8 +31,8 @@ impl Storage for KDGrid<2> { impl Storage for KDGrid<3> { fn is_occupied(&self, position: &Grid3D) -> bool { - let (dist_sq, _) = self.inner.nearest_one(&[position.x as f32, position.y as f32, position.z as f32], &taxicab_grid3).unwrap(); - dist_sq == 0f32 + let a = self.inner.best_n_within(&[position.x as f32, position.y as f32, position.z as f32], 0f32, 1, &taxicab_grid3).unwrap(); + !a.is_empty() } fn deposit(&mut self, position: &Grid3D) { @@ -46,25 +48,22 @@ pub struct KDGrid2Sticker { impl Sticker> for KDGrid2Sticker { fn should_stick(&self, rng: &mut R, space: &KDGrid<2>, position: &Grid2D) -> bool { - let a = space.inner.nearest(&[position.x as f32, position.y as f32], Grid2D::NEIGHBOURS as usize, &taxicab_grid2) + let a = space.inner.best_n_within(&[position.x as f32, position.y as f32], 1f32, Grid2D::NEIGHBOURS as usize, &taxicab_grid2) .unwrap(); if a.len() == 0 { return false; } - let b = a.iter().filter(|(a, _)| *a <= 1f32) - .count(); - let q = (1f32 - self.stick_probability); - let a = q.powi(b as i32); + let a = q.powi(a.len() as i32); rng.gen_range(0f32..1f32) > a } } impl Sticker> for KDGrid2Sticker { fn should_stick(&self, rng: &mut R, space: &KDGrid<3>, position: &Grid3D) -> bool { - let a = space.inner.nearest(&[position.x as f32, position.y as f32, position.z as f32], Grid3D::NEIGHBOURS as usize, &taxicab_grid3) + let a = space.inner.best_n_within(&[position.x as f32, position.y as f32, position.z as f32], 1f32, Grid2D::NEIGHBOURS as usize, &taxicab_grid3) .unwrap(); let q = (1f32 - self.stick_probability);