Commit | Line | Data |
---|---|---|
af18b07f TW |
1 | use std::time::Instant; |
2 | ||
3 | pub struct ScopeTimer { | |
4 | #[allow(dead_code)] | |
5 | start: Instant, | |
6 | #[allow(dead_code)] | |
7 | name: &'static str, | |
8 | } | |
9 | ||
10 | impl ScopeTimer { | |
11 | pub fn new(name: &'static str) -> Self { | |
12 | ScopeTimer { start: Instant::now(), name: name } | |
13 | } | |
14 | } | |
15 | ||
16 | #[cfg(debug_assertions)] | |
17 | impl Drop for ScopeTimer { | |
18 | fn drop(&mut self) { | |
19 | println!("{} took {:?}", self.name, self.start.elapsed()); | |
20 | } | |
21 | } | |
22 | ||
23 | #[macro_export] | |
24 | macro_rules! time_scope { | |
25 | () => { | |
26 | use common::ScopeTimer; | |
27 | let _magical_scope_timer_ = ScopeTimer::new("scope"); | |
28 | }; | |
29 | ( $name:expr ) => { | |
30 | use common::ScopeTimer; | |
31 | let _magical_scope_timer_ = ScopeTimer::new($name); | |
32 | }; | |
33 | } |