time-span 0.1.2

A simple RAII-based time measurement crate
Documentation
  • Coverage
  • 80%
    4 out of 5 items documented3 out of 4 items with examples
  • Size
  • Source code size: 9.16 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.35 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 54s Average build duration of successful builds.
  • all releases: 50s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • paulzhang5511/time-span
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • paulzhang5511

time-span

crates.io GitHub Actions

一个基于 RAII 的简单 Rust 时间测量库。

功能特性

  • 基于 RAII 的时间测量:当守卫离开作用域时自动记录耗时
  • 便捷的宏:避免手动声明变量
  • 闭包支持:测量闭包的耗时并返回其结果
  • 轻量级:仅依赖 log

使用方法

将以下内容添加到你的 Cargo.toml

[dependencies]
time-span = "0.1"
log = "0.4"

基础 RAII 守卫

use time_span::TimeSpan;

fn main() {
    // 初始化日志记录器(例如 env_logger)
    env_logger::init();
    
    {
        let _guard = TimeSpan::new("heavy_work");
        // 执行一些耗时操作...
    } // 守卫被丢弃,时间被记录
}

使用宏

use time_span::time_span;

fn process() {
    time_span!("process_func");
    // 你的代码...
}

测量闭包

use time_span::measure_time;

let result = measure_time("calculation", || {
    // 一些昂贵的计算
    42
});

assert_eq!(result, 42);

许可证

本项目采用以下任一许可证授权:

你可以自行选择。

贡献

除非你明确声明,否则你有意提交以供纳入本作品的任何贡献(如 MIT license 许可证所定义),应按照上述双重许可证进行授权,没有任何额外的条款或条件。