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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
use std::collections::VecDeque;
use std::fmt::{Display, Formatter};
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::{Duration, Instant};
pub const NANOSECONDS_PER_MICROSECOND: u64 = 1_000;
pub const NANOSECONDS_PER_MILLISECOND: u64 = 1_000_000;
pub const NANOSECONDS_PER_SECOND: u64 = 1_000_000_000;
pub const MICROSECONDS_PER_SECOND: u64 = 1_000_000;
pub const MILLISECONDS_PER_SECOND: u64 = 1_000;
pub const SECONDS_PER_MINUTE: u64 = 60;
pub const SECONDS_PER_HOUR: u64 = 3_600;
pub const SECONDS_PER_DAY: u64 = 86_400;
pub const SECONDS_PER_WEEK: u64 = 604_800;
#[derive(Debug, Clone, Copy)]
pub struct Stopwatch {
elapsed: Duration,
started: Option<Instant>,
}
impl Stopwatch {
pub fn new() -> Stopwatch {
Stopwatch {
elapsed: Duration::default(),
started: None,
}
}
pub fn start(&mut self) {
if self.started == None {
self.started = Some(Instant::now());
}
}
pub fn started() -> Stopwatch {
let mut stopwatch = Stopwatch::new();
stopwatch.start();
stopwatch
}
pub fn stop(&mut self) {
if self.started != None {
self.elapsed = self.elapsed();
self.started = None;
}
}
pub fn reset(&mut self) -> Duration {
let elapsed = self.elapsed();
self.elapsed = Duration::default();
self.started = self.started.map(|_| Instant::now());
elapsed
}
pub fn restart(&mut self) {
self.reset();
self.start();
}
pub fn running(&self) -> bool {
self.started != None
}
pub fn elapsed(&self) -> Duration {
match self.started {
Some(started) => started.elapsed() + self.elapsed,
None => self.elapsed,
}
}
pub fn elapsed_seconds(&self) -> u64 {
self.elapsed().as_secs()
}
pub fn elapsed_milliseconds(&self) -> u128 {
self.elapsed().as_millis()
}
pub fn elapsed_microseconds(&self) -> u128 {
self.elapsed().as_micros()
}
pub fn elapsed_nanoseconds(&self) -> u128 {
self.elapsed().as_nanos()
}
}
impl Default for Stopwatch {
fn default() -> Stopwatch {
Stopwatch::new()
}
}
impl Display for Stopwatch {
fn fmt(&self, formatter: &mut Formatter) -> Result<(), std::fmt::Error> {
write!(formatter, "{:?}", self.elapsed())
}
}
pub struct FpsClock {
queue: VecDeque<Instant>,
frames: Arc<AtomicUsize>,
}
impl FpsClock {
pub fn new() -> FpsClock {
FpsClock {
queue: VecDeque::with_capacity(1024),
frames: Arc::new(AtomicUsize::new(0)),
}
}
pub fn update(&mut self) {
let now = Instant::now();
let previous = now - Duration::from_secs(1);
self.queue.push_back(now);
while self.queue.front().map(|x| *x < previous).unwrap_or(false) {
self.queue.pop_front();
}
self.frames.store(self.queue.len(), Ordering::Relaxed);
}
pub fn client(&self) -> Fps {
Fps {
frames: self.frames.clone(),
}
}
pub fn fps(&self) -> usize {
self.frames.load(Ordering::Relaxed)
}
}
pub struct Fps {
frames: Arc<AtomicUsize>,
}
impl Fps {
pub fn value(&self) -> usize {
self.frames.load(Ordering::Relaxed)
}
}