Slow Function Warning
This crate provides a procedural macro to inject timers into functions and print a warning if it takes longer than expected. It can be particularly useful for debugging performance issues during development.
This is not meant to be a benchmarking tool, but rather a way to detect potential performance issues in your code.
For example my use case was for developing a game in Bevy and I've added this to all my systems to detect if any game system function takes longer than a 1ms.
Usage
Installation
Add the following to your Cargo.toml
:
[]
= "0.2.0"
# For wasm targets
[]
= "1"
Basic Example
// Warn if the function takes longer than 1000 milliseconds
The warning is not on by default and is only recommended for debugging purposes. To enable it use the slow_function_warning
feature.
Custom Message Example
// Warn if the function takes longer than a second with a custom message
You can also use the function parameters in your message:
// Warn if the function takes longer than a second with a custom message
Duration Syntax
You can specify the duration using numeric literals followed by a suffix:
ns
for nanosecondsms
for millisecondss
for secondsm
for minutesh
for hoursd
for days
Available Variables
module: String
- The name of the modulefunction: String
- The name of the functionelapsed: Duration
- The elapsed timenanos: u64
- The elapsed time in nanosecondsns: u64
- The elapsed time in nanosecondsmillis: u64
- The elapsed time in millisecondsms: u64
- The elapsed time in millisecondssecs: u64
- The elapsed time in secondss: u64
- The elapsed time in seconds
How it works
This is a procedural macro that takes the content of a function and places it in a closure, executes it and times how long it took.
// Warn if the function takes longer than a second with a custom message
Becomes: