Skip to content

Tiny library for coarse-grained hierarchical profiling in Rust

License

Notifications You must be signed in to change notification settings

leod/coarse-prof

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

33 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

coarse-prof

Docs Status license Crates.io

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.

Usage

Just add this line to your dependencies in Cargo.toml:

coarse-prof = "0.2"

Example

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

About

Tiny library for coarse-grained hierarchical profiling in Rust

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages