From: Tomas Wenström Date: Fri, 29 Jan 2021 20:29:48 +0000 (+0100) Subject: Reorganized mod common and added a smart scope timer X-Git-Url: http://dolda2000.com/gitweb/?p=kaka%2Frust-sdl-test.git;a=commitdiff_plain;h=af18b07f3ff382c0bb122d0e0b235cd7991a2597 Reorganized mod common and added a smart scope timer --- diff --git a/src/common.rs b/src/common/geometry.rs similarity index 100% rename from src/common.rs rename to src/common/geometry.rs diff --git a/src/common/mod.rs b/src/common/mod.rs new file mode 100644 index 0000000..dc8d63b --- /dev/null +++ b/src/common/mod.rs @@ -0,0 +1,8 @@ +mod geometry; +pub use common::geometry::Point2D; +pub use common::geometry::Rect; +pub use common::geometry::Radians; +pub use common::geometry::Degrees; + +mod time; +pub use common::time::ScopeTimer; diff --git a/src/common/time.rs b/src/common/time.rs new file mode 100644 index 0000000..0468d5d --- /dev/null +++ b/src/common/time.rs @@ -0,0 +1,33 @@ +use std::time::Instant; + +pub struct ScopeTimer { + #[allow(dead_code)] + start: Instant, + #[allow(dead_code)] + name: &'static str, +} + +impl ScopeTimer { + pub fn new(name: &'static str) -> Self { + ScopeTimer { start: Instant::now(), name: name } + } +} + +#[cfg(debug_assertions)] +impl Drop for ScopeTimer { + fn drop(&mut self) { + println!("{} took {:?}", self.name, self.start.elapsed()); + } +} + +#[macro_export] +macro_rules! time_scope { + () => { + use common::ScopeTimer; + let _magical_scope_timer_ = ScopeTimer::new("scope"); + }; + ( $name:expr ) => { + use common::ScopeTimer; + let _magical_scope_timer_ = ScopeTimer::new($name); + }; +} diff --git a/src/core/level.rs b/src/core/level.rs index 087af3a..c3f4aae 100644 --- a/src/core/level.rs +++ b/src/core/level.rs @@ -1,4 +1,5 @@ use common::Point2D; +use ::{point, time_scope}; use core::render::Renderer; use noise::{NoiseFn, OpenSimplex, Seedable}; use rand::Rng; @@ -63,6 +64,8 @@ pub struct Grid { impl Grid { fn generate(iterations: u8) -> Grid { + time_scope!("grid generation"); + let cell_size = 20; let (width, height) = (2560 / cell_size, 1440 / cell_size); @@ -189,6 +192,7 @@ impl Grid { } fn find_regions(&self) -> Vec { + time_scope!("finding all regions"); let mut regions = vec!(); let mut marked = vec!(vec!(false; self.height); self.width); for x in 0..self.width {