#time #rate #game #sensor #instrumentation

update_rate

A generic, low-overhead rate counter for FPS counters and the like

5 stable releases

2.0.1 Jun 9, 2020
2.0.0 Jul 30, 2017
1.1.0 Mar 16, 2017
1.0.1 Mar 16, 2017

#67 in Profiling

23 downloads per month

MIT license

18KB
276 lines

update_rate

Build Status docs.rs documented available on crates.io

A generic, low-overhead rate counter for frames-per-second indicators, measurement averaging, and more.

use update_rate::{RateCounter, RollingRateCounter};

let mut c = RollingRateCounter::new(10);

loop {
    c.update();
    mycrate.work(); // Perform the slow operation
    println!("Updating at {}", c); 
}

lib.rs:

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.");

No runtime deps