3个版本
0.1.2 | 2019年10月13日 |
---|---|
0.1.1 | 2019年9月30日 |
0.1.0 | 2019年9月30日 |
#401 在 命令行界面
每月 8,150 次下载
在 2 个Crate中使用
8KB
82 行
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文件。