pub struct DiscreteRateCounter { /* private fields */ }
Expand description
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.
Generally, the RollingRateCounter is a better instrument, but it has to recalculate its measured rate every time it is queried whereas the DiscreteRateCounter only recalculates every n cycles.
§Usage
Call .update()
every time your system starts a new update/cycle; for
instance, an FPS counter would call this at the beginning of every frame.
The sample rate (set with set_sample_rate()
and in the first argument to
new()
) governs how many .update()
calls are required before a
meaningful result is produced.
You can also use .update_immut() for this. Since DiscreteRateCounter is small and easily copyable, this is negligibly less efficient.
Implementations§
Source§impl DiscreteRateCounter
impl DiscreteRateCounter
Sourcepub fn new(samples: u64) -> Self
pub fn new(samples: u64) -> Self
Create a new DiscreteRateCounter which calculates the update rate every
samples
cycles. Until that many cycles occur, rate()
will
return a useless value, typically 0.0.
If this isn’t acceptable, one strategy is to start the samples
value at 0
and keep ramping it up until it reaches your target samples
value;
however, the data near the beginning will not be useful.
Sourcepub fn rate_age_cycles(&self) -> u64
pub fn rate_age_cycles(&self) -> u64
Return the number of cycles since the rate was last recalculated.
Sourcepub fn rate_age_duration(&self) -> Duration
pub fn rate_age_duration(&self) -> Duration
Return the amount of time since the rate was last recalculated. This requires examining the system clock and is thus relatively expensive.
Trait Implementations§
Source§impl Clone for DiscreteRateCounter
impl Clone for DiscreteRateCounter
Source§fn clone(&self) -> DiscreteRateCounter
fn clone(&self) -> DiscreteRateCounter
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl Debug for DiscreteRateCounter
impl Debug for DiscreteRateCounter
Source§impl Display for DiscreteRateCounter
impl Display for DiscreteRateCounter
Source§impl RateCounter for DiscreteRateCounter
impl RateCounter for DiscreteRateCounter
Source§fn samples(&self) -> u64
fn samples(&self) -> u64
Source§fn set_samples(&mut self, samples: u64)
fn set_samples(&mut self, samples: u64)
Source§impl RateCounterImmut for DiscreteRateCounter
impl RateCounterImmut for DiscreteRateCounter
Source§fn update_immut(self) -> Self
fn update_immut(self) -> Self
Consumes the struct and returns an updated version. Call this at the beginning of each cycle of the periodic activity being measured.
§Examples
use update_rate::DiscreteRateCounter;
use update_rate::{RateCounter, RateCounterImmut};
let c = DiscreteRateCounter::new(5);
for i in 1..101 {
let c = c.update_immut();
if i % 10 == 0 {println!("Rate: {}", c.rate())}
// Do work here
}