[−][src]Crate update_rate
This crate provides a utility for counting updates, for instance frame rates.
Implementors of the RateCounter
trait have a
method, .update()
, which is meant to be called every time your system
updates (e.g. every frame, every physics update, etc).
The trait RateCounterImmut
adds an immutable update method which consumes
the rate counter and returns an updated one.
This can also be done immutably using shadowing and .update_immut()
.
Examples
The one important thing to remember is to call your Counter's update()
(or update_immut()
) at the beginning of your cycles.
use update_rate::{RateCounter, DiscreteRateCounter}; // Create a new DiscreteRateCounter with a sample rate of 10 updates let mut c = DiscreteRateCounter::new(10); for _ in 1..11 { c.update(); // Rate should be 100 Hz with 10 ms/update std::thread::sleep(std::time::Duration::from_millis(10)); } let difference = 100.0 - c.rate(); println!("Rate was {}", c.rate()); assert!(difference < 10.0, "Counter rate should be closer to actual rate.");
Structs
DiscreteRateCounter | A very basic non-rolling update counter. It counts n updates, calculates, and then resets (where n is the sample rate), which means that it takes at least n updates to react to a change in rate appropriately. |
RollingRateCounter | A rolling update counter. It records as many updates as the given sample rate and re-calculates the average update time on each call to update. |
Traits
RateCounter | Basic rate counter functionality. |
RateCounterImmut | Immutable extensions for rate counters. |