Git Product home page Git Product logo

took-rs's Introduction

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.

took-rs's People

Contributors

timvisee avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.