Galactica/crates/util/src/timing.rs

66 lines
1.5 KiB
Rust

//! Keep track of the time we spent in each part of the game loop.
use std::time::{Duration, Instant};
/// Utility struct.
/// Keeps track of the time we spent in each part of the game loop.
#[derive(Debug, Clone)]
pub struct Timing {
/// The time we spent on all frame computations
pub frame: Duration,
frame_timer: Instant,
/// The time we spent simulating physics
pub physics_sim: Duration,
physics_sim_timer: Instant,
/// The time we spent updating physics ships
pub physics_ship: Duration,
physics_ship_timer: Instant,
}
// TODO: document each duration
impl Timing {
/// Create a new timing struct
pub fn new() -> Self {
Self {
frame: Duration::ZERO,
physics_sim: Duration::ZERO,
physics_ship: Duration::ZERO,
physics_sim_timer: Instant::now(),
physics_ship_timer: Instant::now(),
frame_timer: Instant::now(),
}
}
/// Start frame timer
pub fn start_frame(&mut self) {
self.frame_timer = Instant::now();
}
/// Start physics sim timer
pub fn start_physics_sim(&mut self) {
self.physics_sim_timer = Instant::now();
}
/// Start physics ship timer
pub fn start_physics_ships(&mut self) {
self.physics_ship_timer = Instant::now();
}
/// Record total frame compute time
pub fn mark_frame(&mut self) {
self.frame = self.frame_timer.elapsed();
}
/// Record physics simulation time
pub fn mark_physics_sim(&mut self) {
self.physics_sim = self.physics_sim_timer.elapsed();
}
/// Record physics ship update time
pub fn mark_physics_ships(&mut self) {
self.physics_ship = self.physics_ship_timer.elapsed();
}
}