Renamed common to util
[kaka/rust-sdl-test.git] / src / util / time.rs
diff --git a/src/util/time.rs b/src/util/time.rs
new file mode 100644 (file)
index 0000000..b533bb6
--- /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 }
+    }
+}
+
+#[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 util::ScopeTimer;
+        let _magical_scope_timer_ = ScopeTimer::new("scope");
+    };
+    ( $name:expr ) => {
+       use util::ScopeTimer;
+        let _magical_scope_timer_ = ScopeTimer::new($name);
+    };
+}