-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimer.rs
45 lines (39 loc) · 904 Bytes
/
timer.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
use time;
use std::cmp::{min, max};
pub struct Stats {
times_called: u64,
min: u64,
max: u64,
cumul: u64,
}
impl Stats {
// Only used when a print_stats! macro is invoked.
#[allow(dead_code)]
pub fn avg(&self) -> u64 {
if self.times_called != 0 {
self.cumul/self.times_called
} else {
0
}
}
}
#[allow(dead_code)]
pub struct Stopwatch {
result_ptr: &'static mut Stats,
start: u64,
}
impl Stopwatch {
#[allow(dead_code)]
pub fn new(stats: &'static mut Stats) -> Stopwatch {
Stopwatch { result_ptr: stats, start: time::precise_time_ns() }
}
}
impl Drop for Stopwatch {
fn drop(&mut self) {
let result = time::precise_time_ns()-self.start;
self.result_ptr.times_called += 1;
self.result_ptr.min = min(self.result_ptr.min, result);
self.result_ptr.max = max(self.result_ptr.max, result);
self.result_ptr.cumul += result;
}
}