#execution-time #measuring #nanosecond #expression #measurement #precision #measure

tempus_fugit

A tiny library to measure the execution time of Rust expressions, with nanosecond precision

16 releases (9 breaking)

0.11.0 Jul 8, 2020
0.9.0 Mar 27, 2020
0.8.2 Nov 18, 2019
0.5.0 Sep 14, 2018
0.3.1 Mar 7, 2018

#153 in Profiling

48 downloads per month

MIT/Apache

15KB
268 lines

Tempus Fugit   Latest Version Rustc Version 1.26+

This is a Rust crate that operates around the concept of measuring the time it takes to take some action.

Convenience is the name of the game here, specifically by empowering a dependent crate to do 2 things:

  1. Measuring the wall-clock time of any expression in nanosecond[1] resolution:

    [dependencies]
    tempus_fugit = "0.10"
    
    #[macro_use] extern crate tempus_fugit;
    
    use std::fs::File;
    use std::io::Read;
    use tempus_fugit::Measurement;
    
    fn main() {
        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")
        }};
    
        println!("contents: {:?}", contents);
        println!("opening and reading file took {}", measurement);
    }
    

    The measure! macro returns a tuple containing the result of executing an expression (in this case a block), as well as a Measurement which indicates how long the expression took to execute.

  2. Displaying a Measurement in a human-readable fashion. There is a Display impl for Measurement, so this is as easy as formatting a value with e.g. format!("{}", measurement).

The Measurement type also has impls for Ord and Eq, which makes comparison and sorting easy.

In addition, there is opt-in support for de/serialization through Serde. This is activated by using the follwing in your crate's Cargo.toml:

[dependencies]
tempus_fugit = { version = "0.10", features = ["enable_serde"] }

[1] While the accounting is in nanosecond resolution, the actual resolution may be limited to courser granularity by the operating system.

Documentation

The API docs are located here.

Dependencies

~1–2MB
~33K SLoC