Attempt to speed up and add some logging

This commit is contained in:
Joshua Coles 2023-03-09 08:27:34 +00:00
parent 9f74ec2ef0
commit 58f19cdb9a
2 changed files with 21 additions and 11 deletions

View File

@ -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<R: Rng, P: Position, S: Storage<P>, W: Walker<P>, Sp: Spawne
max_frames: Option<usize>,
notify_every: Option<usize>,
) {
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();
}

View File

@ -17,8 +17,10 @@ pub struct KDGrid<const N: usize> {
impl Storage<Grid2D> 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<Grid2D> for KDGrid<2> {
impl Storage<Grid3D> 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<Grid2D, KDGrid<2>> for KDGrid2Sticker {
fn should_stick<R: Rng>(&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<Grid3D, KDGrid<3>> for KDGrid2Sticker {
fn should_stick<R: Rng>(&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);