Skip to content

Instantly share code, notes, and snippets.

@toyboot4e
Created August 6, 2021 21:36
Show Gist options
  • Select an option

  • Save toyboot4e/7a597fcfb2232504dd82d10a4094bad0 to your computer and use it in GitHub Desktop.

Select an option

Save toyboot4e/7a597fcfb2232504dd82d10a4094bad0 to your computer and use it in GitHub Desktop.
Simple FPS watcher
/// Simple FPS watchcer
#[derive(Debug, Clone, Default)]
pub struct Fps {
/// Average duration per frame in seconds
avg: f64,
/// Square of spike FPS in seconds
spike: f64,
}
impl Fps {
const K: f64 = 0.05;
/// Update FPS counts with real time
pub fn update(&mut self, frame_time: Duration) {
self.update_avg(frame_time.as_secs_f64());
self.update_spike(frame_time.as_secs_f64());
}
/// Average FPS
pub fn avg(&self) -> f64 {
1.0 / self.avg
}
/// Spike FPS
pub fn spike(&self) -> f64 {
self.spike.sqrt()
}
fn update_avg(&mut self, dt: f64) {
self.avg *= 1.0 - Self::K;
self.avg += dt * Self::K;
}
// FIXME:
fn update_spike(&mut self, dt: f64) {
self.spike *= 1.0 - Self::K;
self.spike += (dt * dt) * Self::K;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment