Initial commit
[Mitarbeiter/Tim-Zeitz/stud-rust-base.git] / src / time.rs
1 use time_crate as time;
2
3 #[allow(dead_code)]
4 pub fn report_time<Out, F: FnOnce() -> Out>(name: &str, f: F) -> Out {
5     let start = time::now();
6     println!("starting {}", name);
7     let res = f();
8     println!("done {} - took: {}", name, (time::now() - start));
9     res
10 }
11
12 #[allow(dead_code)]
13 pub fn measure<Out, F: FnOnce() -> Out>(f: F) -> (Out, time::Duration) {
14     let start = time::now();
15     let res = f();
16     (res, time::now() - start)
17 }
18
19 #[allow(dead_code)]
20 #[derive(Debug)]
21 pub struct Timer {
22     start: time::Tm
23 }
24
25 impl Timer {
26     #[allow(dead_code)]
27     pub fn new() -> Timer {
28         Timer { start: time::now() }
29     }
30
31     #[allow(dead_code)]
32     pub fn restart(&mut self) {
33         self.start = time::now();
34     }
35
36     #[allow(dead_code)]
37     pub fn report_passed_ms(&self) {
38         println!("{}ms", (time::now() - self.start).num_milliseconds());
39     }
40
41     #[allow(dead_code)]
42     pub fn get_passed_ms(&self) -> i64 {
43         (time::now() - self.start).num_milliseconds()
44     }
45 }