Scope-based single and multithreaded profiling.

Overview

Profi

Crates.io Version

A simple profiler for single and multithreaded applications.

Record the time it takes for a scope to end and print the timings when the program exits.

Each measurement has an overhead of ~25ns-50ns, so it shouldn't impact benchmarks.
Run the benchmarks example to see what's the overhead on your machine.

Setup

profi is controlled by the enable feature, which is active by default.
When disabled, all macros and methods will become no-ops, resulting in zero impact on your code.

To disable it, add default-features = false to the profi dependency in your Cargo.toml.

For convenience, you can also add a custom feature:

[dependencies]
profi = { version = "*", default-features = false }

[features]
prof = ["profi/enable", "profi/attributes"]

And run it with cargo run --release --features prof

If you use rayon, enable the rayon feature!

Usage

See the examples for more usage cases.

Basic Usage

use profi::{prof, print_on_exit};

fn main() {
 // Prints the timings to stdout when the program exits
 // Always put at the top of the main function to ensure it's dropped last
 //
 // An implicit `main` guard is created to profile the whole application
 print_on_exit!();

 // Sleep for a bit to simulate some work
 std::thread::sleep(std::time::Duration::from_millis(200));
}

codeimage-snippet_30

Loops

use profi::{prof, print_on_exit};

fn main() {
  print_on_exit!();

  for _ in 0..100 {
      prof!(iteration);
      std::thread::sleep(std::time::Duration::from_millis(10));
  }
}

codeimage-snippet_31

Multiple threads

use profi::{print_on_exit, prof_guard};

fn do_work(i: usize) {
    // Need to bind it to a variable to ensure it isn't dropped before sleeping
    let _guard = if i < 6 {
        prof_guard!("6 first")
    } else {
        prof_guard!("4 last")
    };
    std::thread::sleep(std::time::Duration::from_millis(10));
    // The guard goes out of scope here
}

fn main() {
    print_on_exit!();
    
    // Spawn 10 threads
    std::thread::scope(|s| {
        for i in 0..10 {
            s.spawn(move || {
                do_work(i);
            });
        }
    });
}

codeimage-snippet_32

"CPU Time" is the combined time all threads have spent on that scope.

For example, "6 first" has a "CPU Time" of 60 milliseconds because each thread waits 10ms, and the program spawns six of them.

Attribute

Enable the attributes feature to use the profile attribute on functions.
This will add a guard at the start of the function.

use profi::profile;

#[profile]
fn anotated() { /* ... */ }

Features

Name Description
enable Activates the profiling, if not active all macros become no-ops
attributes Enables the #[prof] macro
deep-hierarchy By default profi merges all uses of a function, use this feature to disable this behaviour.
See the nested example for more information
nightly Enables nightly-only optimizations (unused at the moment)
rayon Necessary if using rayon
You might also like...
🏪 Modern emoji picker popup for desktop, based on Emoji Mart, built with Tauri and Svelte
🏪 Modern emoji picker popup for desktop, based on Emoji Mart, built with Tauri and Svelte

Emoji Mart desktop popup Modern emoji picker popup app for desktop, based on the amazing Emoji Mart web component. 🍾 Built as a popup: quick invocati

A lending iterator trait based on generic associated types and higher-rank trait bounds

A lending iterator trait based on higher-rank trait bounds (HRTBs) A lending iterator is an iterator which lends mutable borrows to the items it retur

A merkle-based token distributor for the Solana network that allows distributing a combination of unlocked and linearly unlocked tokens.

merkle-distributor A program for distributing tokens efficiently via uploading a Merkle root. Claiming Airdrop via CLI To claim via CLI instead of usi

A DIY, IMU-based skateboard activity tracker
A DIY, IMU-based skateboard activity tracker

tracksb A DIY, IMU-based skateboard activity tracker. The idea is to come up with algorithms to track activity during skateboarding sessions. A compan

A tiling window manager for Windows 10 based on binary space partitioning
A tiling window manager for Windows 10 based on binary space partitioning

yatta BSP Tiling Window Manager for Windows 10 Getting Started This project is still heavily under development and there are no prebuilt binaries avai

A high level diffing library for rust based on diffs
A high level diffing library for rust based on diffs

Similar: A Diffing Library Similar is a dependency free crate for Rust that implements different diffing algorithms and high level interfaces for it.

wasm actor system based on lunatic

Wactor WASM actor system based on lunatic. Actors run on isolated green threads. They cannot share memory, and communicate only through input and outp

a hobby OS for x86_64 based on MikanOS.

a hobby OS for x86_64 based on MikanOS.

Nannou/Rust tutorial based on Schotter by Georg Nees
Nannou/Rust tutorial based on Schotter by Georg Nees

Schotter (German for gravel) is a piece by computer art pioneer Georg Nees. It consists of a grid of squares 12 across and 22 down with random rotation and displacement that increases towards the bottom.

A self-contained, single-binary Rust and Leptos application for remote Wake-on-LAN

Remote Wake-on-LAN with Rust and Leptos A self-contained, single-binary Rust and Leptos application serving a web interface to wake another device on

Valentin Bersier 6 Jan 28, 2023
single file, std only, async Rust executor

whorl - A single file, std only, async Rust executor whorl was created to teach you how async executors work in Rust. It is not the fastest executor n

Michael Gattozzi 459 Dec 29, 2022
An opinionated Rust library for interacting with AWS DynamoDB single-table designs.

Modyne An opinionated library for interacting with AWS DynamoDB single-table designs. † Motive Modyne follows the precepts laid out for effective sing

Marcus Griep 14 Jun 8, 2023
DynamoDB library for single-table design in Rust

A DynamoDB abstraction for Rust Deez is a DynamoDB abstraction for implementing Single Table Design easily, inspired by ElectroDB. Getting Started Def

null 10 Jul 2, 2023
A tool that transforms an IMVU badge canvas into a single cohesive image.

IMVU Badge Canvas Image Generator This tool fetches an IMVU user's badge canvas and creates a single unified image representation of it. Instead of ha

Helba 3 Sep 20, 2023
Utilities and tools based around Amazon S3 to provide convenience APIs in a CLI

s3-utils Utilities and tools based around Amazon S3 to provide convenience APIs in a CLI. This tool contains a small set of command line utilities for

Isaac Whitfield 47 Dec 15, 2022
A low-ish level tool for easily writing and hosting WASM based plugins.

A low-ish level tool for easily writing and hosting WASM based plugins. The goal of wasm_plugin is to make communicating across the host-plugin bounda

Alec Deason 62 Sep 20, 2022
CBOR (binary JSON) for Rust with automatic type based decoding and encoding.

THIS PROJECT IS UNMAINTAINED. USE serde_cbor INSTEAD. This crate provides an implementation of RFC 7049, which specifies Concise Binary Object Represe

Andrew Gallant 121 Dec 27, 2022
Propositional logic evaluator and rule-based pattern matcher

Plogic Propositional logic evaluator and pattern transformer written in Rust. Plogic evaluates logic expressions in a REPL (Read, Execute, Print, Loop

Jan 17 Nov 25, 2022
Generate commit messages using GPT3 based on your changes and commit history.

Commit Generate commit messages using GPT-3 based on your changes and commit history. Install You need Rust and Cargo installed on your machine. See t

Brian Le 40 Jan 3, 2023