A simple Rust timer macro library for measuring execution time of code blocks.
- Easy to use macro syntax for timing code blocks
- Two types of timers:
timer!- Prints timing information to stdout (only in debug builds by default)timer_silent!- Returns timing information without printing (always active)
- Multiple syntax options for flexibility
- Zero overhead in release builds (unless
release_alsofeature is enabled)
Add this to your Cargo.toml:
[dependencies]
quick-timer = "0.1.0"use quick_timer::timer;
fn main() {
timer! {
println!("Hello, world!");
}
}use quick_timer::timer;
fn main() {
// With a tag
timer!(# "My Function" {
// Some expensive operation
std::thread::sleep(std::time::Duration::from_millis(100));
});
// Alternative syntax
timer! {
# "Another Function"
println!("Doing some work...");
}
}use quick_timer::timer;
fn main() {
let result = timer! {
1 + 1
};
assert_eq!(result, 2);
}use quick_timer::timer_silent;
fn main() {
let (result, duration) = timer_silent! {
println!("This will be printed");
42
};
assert_eq!(result, 42);
println!("Execution took {} ms", duration.as_millis());
}By default, timer! macro only works in debug builds. To enable it in release builds as well, enable the release_also feature:
[dependencies]
quick-timer = { version = "0.1.0", features = ["release_also"] }The timer! macro supports multiple syntax variants:
use quick_timer::timer;
fn main() {
// Basic form
timer! { /* code */ }
// With block syntax
timer!(block: { /* code */ });
// With tag
timer!(# "Tag" { /* code */ });
// With identifier tag
timer!(# Tag { /* code */ });
// Explicit tag syntax
timer!(tag: "Tag", block: { /* code */ });
// Braceless forms
timer! { # "Tag" /* code */ }
}This project is licensed under either of
at your option.