| Crates.io | log-execution-time |
| lib.rs | log-execution-time |
| version | 0.1.0 |
| created_at | 2025-01-01 07:37:49.08669+00 |
| updated_at | 2025-01-01 07:37:49.08669+00 |
| description | A Rust procedural macro to log the execution time of functions. |
| homepage | https://github.com/satishbabariya/log-execution-time-rs |
| repository | https://github.com/satishbabariya/log-execution-time-rs |
| max_upload_size | |
| id | 1500649 |
| size | 6,075 |
log_execution_time is a Rust procedural macro that logs the execution time of functions. It leverages the log crate to provide detailed timing information, making it a useful tool for performance monitoring and debugging.
Add the following to your Cargo.toml file:
[dependencies]
log_execution_time = "0.1.0"
log = "0.4" # Required for logging
Ensure you have a logger initialized in your project, such as env_logger or any other compatible logger.
Annotate your functions with #[log_execution_time]:
use log_execution_time::log_execution_time;
#[log_execution_time]
fn compute() {
// Perform some computation
let sum: u32 = (1..=1000).sum();
println!("Sum is: {}", sum);
}
fn main() {
env_logger::init();
compute();
}
The macro also works with asynchronous functions:
use log_execution_time::log_execution_time;
#[log_execution_time]
async fn fetch_data() {
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
println!("Data fetched!");
}
#[tokio::main]
async fn main() {
env_logger::init();
fetch_data().await;
}
Create an example file in your project to test the macro:
mkdir examples
Add the following to examples/main.rs:
use log_execution_time::log_execution_time;
#[log_execution_time]
fn example_function() {
let product: u32 = (1..=10).product();
println!("Product is: {}", product);
}
fn main() {
env_logger::init();
example_function();
}
Run the example:
cargo run --example main
This project is licensed under the MIT License. See the LICENSE file for details.