Crates.io | coarse-prof |
lib.rs | coarse-prof |
version | 0.2.6 |
source | src |
created_at | 2019-12-03 21:25:56.20637 |
updated_at | 2023-06-03 15:14:38.329732 |
description | Tiny library for coarse-grained hierarchical profiling |
homepage | https://github.com/leod/coarse-prof |
repository | https://github.com/leod/coarse-prof |
max_upload_size | |
id | 186224 |
size | 18,967 |
coarse-prof
allows you to hierarchically measure the time that blocks in
your program take, enabling you to get an intuition of where most time is
spent. This can be useful for game development, where you have a bunch of
things that need to run in every frame, such as physics, rendering,
networking and so on, and you may wish to identify the hot spots, so that
you know whether and what to optimize.
coarse-prof
's implementation has been inspired by
hprof.
In contrast to hprof
, which resets measurements after each frame, this
library tracks averages over multiple frames. Also, coarse-prof
provides
the macro profile
for profiling a scope, so that users do not have to assign a
name to scope guards.
Just add this line to your dependencies in Cargo.toml
:
coarse-prof = "0.2"
use std::thread::sleep;
use std::time::Duration;
use coarse_prof::profile;
fn render() {
profile!("render");
// So slow!
sleep(Duration::from_millis(10));
}
// Our game's main loop
let num_frames = 100;
for i in 0..num_frames {
profile!("frame");
// Physics don't run every frame
if i % 10 == 0 {
profile!("physics");
sleep(Duration::from_millis(2));
{
profile!("collisions");
sleep(Duration::from_millis(1));
}
}
render();
}
// Print the profiling results.
coarse_prof::write(&mut std::io::stdout()).unwrap();
Example output:
frame: 100.00%, 10.40ms/call @ 96.17Hz
physics: 3.04%, 3.16ms/call @ 9.62Hz
collisions: 33.85%, 1.07ms/call @ 9.62Hz
render: 96.84%, 10.07ms/call @ 96.17Hz