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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
use std::fmt;
use std::time::{Duration, Instant};
#[derive(Clone, Debug)]
pub struct Timer {
start: Instant,
}
impl Timer {
pub fn new() -> Self {
Self::default()
}
pub fn took(&self) -> Took {
Took::from_std(self.start.elapsed())
}
}
impl Default for Timer {
fn default() -> Self {
Self {
start: Instant::now(),
}
}
}
impl fmt::Display for Timer {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.took().fmt(f)
}
}
#[derive(Clone, Debug)]
pub struct Took {
elapsed: Duration,
}
impl Took {
pub fn from_std(elapsed: Duration) -> Self {
Self { elapsed }
}
pub fn into_std(self) -> Duration {
self.elapsed
}
pub fn describe(&self, description: &str) {
println!("{} took {}", description, self);
}
}
impl fmt::Display for Took {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let elapsed = self.elapsed;
let secs = u128::from(elapsed.as_secs());
let millis = elapsed.as_millis();
let micros = elapsed.as_micros();
let nanos = elapsed.as_nanos();
let (major, minor, t) = if secs > 0 {
(secs, millis, "s")
} else if millis > 0 {
(millis, micros, "ms")
} else if micros > 0 {
(micros, nanos, "μs")
} else {
(nanos, nanos * 1000, "ns")
};
let time = major as f64 + (minor - major * 1000) as f64 / 1000.0;
f.pad(&format!("{:.2} {}", time, t))
}
}
#[must_use]
pub fn took<T, F>(f: F) -> (Took, T)
where
F: FnOnce() -> T,
{
let take = Timer::new();
let out = f();
(take.took(), out)
}
#[must_use]
pub fn took_print<T, F>(description: &str, f: F) -> T
where
F: FnOnce() -> T,
{
let (took, out) = took(f);
eprintln!("{} took {}", description, took);
out
}