Tempus Fugit: A tiny library to measure the runtime of Rust expressions

I’d like to introduce the tempus_fugit library crate, which makes it easy to gather and display runtime performance data for Rust expressions.

What does this mean? Well, it allows you to measure how long it takes to execute an expression:

let (contents, measurement) = measure! {{
        let mut file = File::open("Cargo.lock").expect("failed to open Cargo.lock");
        let mut contents = vec![];
        file.read_to_end(&mut contents).expect("failed to read Cargo.lock");
        String::from_utf8(contents).expect("failed to extract contents to String")

Assuming the measure! macro invocation completes without issue, contents will contain the value returned by the block passed to measure!, and measurement is an opaque object that implements Display and is thus easily printable.

For example, if we then were to print out measurement:

println!("measurement: {}", measurement);

Then we get an output like:

measurement: 794 µs 270 ns

How this works is that the time duration is divided up into a most significant section (represented as 794 µs above) and a least significant section (the 270 ns part). Thus, it’s easy to get an idea of how large the measurement actually is, on a human level. This scales all the way up to and including xxx h yyy m for operations in such a time scale.

I now use it in lots of places to keep track of runtime performance, as knowledge is power in this regard.
Hopefully it will find similar use in the Rust community as it already has for me.