3 releases

0.1.2 Oct 13, 2019
0.1.1 Sep 30, 2019
0.1.0 Sep 30, 2019

#299 in Development tools

Download history 983/week @ 2021-10-01 984/week @ 2021-10-08 931/week @ 2021-10-15 950/week @ 2021-10-22 1033/week @ 2021-10-29 738/week @ 2021-11-05 713/week @ 2021-11-12 1278/week @ 2021-11-19 803/week @ 2021-11-26 728/week @ 2021-12-03 1013/week @ 2021-12-10 1112/week @ 2021-12-17 1428/week @ 2021-12-24 1299/week @ 2021-12-31 1812/week @ 2022-01-07 1561/week @ 2022-01-14

6,497 downloads per month
Used in took-macro

MIT license

7KB
82 lines

Build status on GitLab CI Newest release on crates.io Documentation Number of downloads on crates.io Project license

took: easily measure & report elapsed time

I always find measuring and reporting run time of code it in a human readable format troublesome.

This crate provides a few simple interfaces to do just that.

Examples

  • Measure & report manually using Timer stopwatch:

    use took::Timer;
    
    let timer = Timer::new();
    // Run heavy task
    println!("Done! Took {}", timer.took());
    
    // Prints:
    // Done! Took 1.00 s
    
  • Measure a function, report manually:

    use took::took;
    
    let (took, result) = took(|| {
        // Run heavy task
    });
    println!("Done, took {}", took);
    
    // Prints:
    // Done! Took 1.00 s
    
  • Measure & report a function automatically using attribute:

    #[macro_use]
    extern crate took_macro;
    
    my_function();
    other_function();
    
    #[took]
    pub fn my_function() {
        // Run heavy task
    }
    
    #[took(description = "Render finished,")]
    pub fn other_function() {
        // Run heavy task
    }
    
    // Prints:
    // my_function() took 1.00 s
    // Render finished, took 1.00 s
    

Requirements

  • Rust 1.33 or newer (with std)

Usage

Add the dependencies in your Cargo.toml. The took-macro dependency is only required if you'll be using the #[took] attribute macro.

[dependencies]
took = "0.1"
took-macro = "0.1" # if using macros

Import and start using:

use took::{Timer, took};

let timer = Timer::new();
println!("Done! Took {}", timer.took());

let (took, result) = took(|| {
    // Run heavy task
});
println!("Done, took {}", took);

If you'll be using #[took] attribute macro, explicitly import it:

#[macro_use]
extern crate took_macro;

#[took]
pub fn function_one() {}

#[took(description = "Some heavy logic finished,")]
pub fn function_two() {}

TODO

  • Support #[took] attribute for almost anything (function call, blocks, if-statements, ...)
  • Time formatting configurability
  • Use more precise timers
  • Print elapsed time to more than just stderr

License

This project is released under the MIT license. Check out the LICENSE file for more information.

No runtime deps

P]