Reorganized mod common and added a smart scope timer
authorTomas Wenström <tomas.wenstrom@gmail.com>
Fri, 29 Jan 2021 20:29:48 +0000 (21:29 +0100)
committerTomas Wenström <tomas.wenstrom@gmail.com>
Fri, 29 Jan 2021 20:29:48 +0000 (21:29 +0100)
src/common/geometry.rs [moved from src/common.rs with 100% similarity]
src/common/mod.rs [new file with mode: 0644]
src/common/time.rs [new file with mode: 0644]
src/core/level.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 (file)
index 0000000..dc8d63b
--- /dev/null
@@ -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 (file)
index 0000000..0468d5d
--- /dev/null
@@ -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);
+    };
+}
index 087af3a..c3f4aae 100644 (file)
@@ -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<Region> {
+       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 {