Execution Time
This Rust project provides a simple way to measure and display the execution time of code blocks. It allows you to start a timer and then print the elapsed time in a human-readable format, including days, hours, minutes, and seconds.
Usage
-
Add the dependency to your
Cargo.toml
file:[] = "0.2"
-
Import and Use the library in your main.rs file.
use ExecutionTime;
-
Examples
To run the examples and see the output for different elapsed times:
- Clone the repository and run the tests:
git clone https://github.com/claudiofsr/execution-time.git cd execution-time cargo test -- --show-output
- The output:
---- tests::elapsed_time_more_than_nanosecond stdout ---- duration: 57ns time: Time { days: 0, hours: 0, minutes: 0, seconds: 5.7e-8, } formatted_output: 0.000000057 second (57ns) ---- tests::elapsed_time_more_than_microsecond stdout ---- duration: 80.057µs time: Time { days: 0, hours: 0, minutes: 0, seconds: 8.0057e-5, } formatted_output: 0.000080057 second (80.057µs) ---- tests::elapsed_time_more_than_millisecond stdout ---- duration: 15.2ms time: Time { days: 0, hours: 0, minutes: 0, seconds: 0.0152, } formatted_output: 0.015200 second (15.2ms) ---- tests::elapsed_time_more_than_second stdout ---- duration: 5.080012045s time: Time { days: 0, hours: 0, minutes: 0, seconds: 5.080012045, } formatted_output: 5.080 seconds (5.080012045s) ---- tests::elapsed_time_more_than_minute stdout ---- duration: 65.000012345s time: Time { days: 0, hours: 0, minutes: 1, seconds: 5.000012345, } formatted_output: 1 minute, 5.000 seconds (65.000012345s) ---- tests::elapsed_time_more_than_hour stdout ---- duration: 3700.05689173s time: Time { days: 0, hours: 1, minutes: 1, seconds: 40.05689173, } formatted_output: 1 hour, 1 minute, 40.057 seconds (3700.05689173s) ---- tests::elapsed_time_more_than_day stdout ---- duration: 93928.03s time: Time { days: 1, hours: 2, minutes: 5, seconds: 28.03, } formatted_output: 1 day, 2 hours, 5 minutes, 28.030 seconds (93928.03s)