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
:
[]
= ["dep:slow_function_warning"]
[]
# Add as a feature to avoid affecting the LSP.
= { = "0.6.0", = true }
# 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 timeelapsed_str: String
- The elapsed time using the limit unit specified (defaults to milliseconds)elapsed_ns: u128
- The elapsed time in nanosecondselapsed_nanos: u128
- The elapsed time in nanosecondselapsed_nanoseconds: u128
- The elapsed time in nanosecondselapsed_us: u128
- The elapsed time in microsecondselapsed_micros: u128
- The elapsed time in microsecondselapsed_microseconds: u128
- The elapsed time in microsecondselapsed_ms: u128
- The elapsed time in millisecondselapsed_millis: u128
- The elapsed time in millisecondselapsed_milliseconds: u128
- The elapsed time in millisecondselapsed_s: u64
- The elapsed time in secondselapsed_secs: u64
- The elapsed time in secondselapsed_seconds: u64
- The elapsed time in secondselapsed_m: u64
- The elapsed time in minuteselapsed_min: u64
- The elapsed time in minuteselapsed_minutes: u64
- The elapsed time in minuteselapsed_h: u64
- The elapsed time in hourselapsed_hours: u64
- The elapsed time in hourselapsed_d: u64
- The elapsed time in dayselapsed_days: u64
- The elapsed time in dayslimit: Duration
- The name of the modulelimit_str: String
- The limit time using the limit unit specified (defaults to milliseconds)limit_ns: u128
- The limit time in nanosecondslimit_nanos: u128
- The limit time in nanosecondslimit_nanoseconds: u128
- The limit time in nanosecondslimit_us: u128
- The limit time in microsecondslimit_micros: u128
- The limit time in microsecondslimit_microseconds: u128
- The limit time in microsecondslimit_ms: u128
- The limit time in millisecondslimit_millis: u128
- The limit time in millisecondslimit_milliseconds: u128
- The limit time in millisecondslimit_s: u64
- The limit time in secondslimit_secs: u64
- The limit time in secondslimit_seconds: u64
- The limit time in secondslimit_m: u64
- The limit time in minuteslimit_min: u64
- The limit time in minuteslimit_minutes: u64
- The limit time in minuteslimit_h: u64
- The limit time in hourslimit_hours: u64
- The limit time in hourslimit_d: u64
- The limit time in dayslimit_days: u64
- The limit time in days
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: