3个版本

0.1.2 2019年10月13日
0.1.1 2019年9月30日
0.1.0 2019年9月30日

#401命令行界面

Download history 1684/week @ 2024-03-17 1823/week @ 2024-03-24 1706/week @ 2024-03-31 1591/week @ 2024-04-07 2166/week @ 2024-04-14 2627/week @ 2024-04-21 1941/week @ 2024-04-28 1806/week @ 2024-05-05 1865/week @ 2024-05-12 2222/week @ 2024-05-19 2536/week @ 2024-05-26 1345/week @ 2024-06-02 2128/week @ 2024-06-09 2514/week @ 2024-06-16 2339/week @ 2024-06-23 1100/week @ 2024-06-30

每月 8,150 次下载
2 个Crate中使用

MIT 许可证

8KB
82

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

took: 轻松测量和报告已过时间

我总是觉得在人类可读的格式下测量和报告代码运行时间很麻烦。

这个Crate提供了一些简单的接口来实现这一点。

示例

  • 使用计时器秒表手动测量和报告

    use took::Timer;
    
    let timer = Timer::new();
    // Run heavy task
    println!("Done! Took {}", timer.took());
    
    // Prints:
    // Done! Took 1.00 s
    
  • 测量函数并手动报告

    use took::took;
    
    let (took, result) = took(|| {
        // Run heavy task
    });
    println!("Done, took {}", took);
    
    // Prints:
    // Done! Took 1.00 s
    
  • 使用属性自动测量和报告函数

    #[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
    

需求

  • Rust 1.33或更高版本(带std

用法

在您的Cargo.toml中添加依赖项。如果需要使用#[took]属性宏,则需要took-macro依赖项。

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

导入并开始使用

use took::{Timer, took};

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

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

如果您将使用#[took]属性宏,则显式导入它

#[macro_use]
extern crate took_macro;

#[took]
pub fn function_one() {}

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

待办事项

  • 支持几乎所有内容的#[took]属性(函数调用、块、if语句等)
  • 时间格式配置
  • 使用更精确的计时器
  • 将已过时间打印到不仅仅是stderr

许可证

本项目根据MIT许可证发布。有关更多信息,请参阅LICENSE文件。

无运行时依赖