#game #instrumentation #hierarchical #optimization #profile #frame

coarse-prof

Tiny library for coarse-grained hierarchical profiling

6 releases

0.2.6 Jun 3, 2023
0.2.5 Jan 1, 2022
0.2.4 Jan 20, 2020
0.2.3 Dec 6, 2019
0.1.0 Dec 3, 2019

#213 in Profiling

Download history 49/week @ 2024-07-25 408/week @ 2024-08-01 313/week @ 2024-08-08 489/week @ 2024-08-15 870/week @ 2024-08-22 533/week @ 2024-08-29 630/week @ 2024-09-05 206/week @ 2024-09-12 83/week @ 2024-09-19 46/week @ 2024-09-26 26/week @ 2024-10-03 27/week @ 2024-10-10 5/week @ 2024-10-17 15/week @ 2024-10-24 73/week @ 2024-10-31 34/week @ 2024-11-07

133 downloads per month
Used in rendology

MIT license

17KB
244 lines

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

Dependencies

~105KB