35个版本

0.1.34 2024年2月9日
0.1.33 2023年11月2日
0.1.29 2023年10月4日
0.1.23 2023年2月24日
0.1.3 2016年11月19日

#7 in 日期和时间

Download history 70776/week @ 2024-04-15 74767/week @ 2024-04-22 61709/week @ 2024-04-29 64482/week @ 2024-05-06 68981/week @ 2024-05-13 85320/week @ 2024-05-20 82557/week @ 2024-05-27 82862/week @ 2024-06-03 77652/week @ 2024-06-10 77773/week @ 2024-06-17 83367/week @ 2024-06-24 68590/week @ 2024-07-01 59039/week @ 2024-07-08 60607/week @ 2024-07-15 63140/week @ 2024-07-22 59814/week @ 2024-07-29

245,381 每月下载量
162 个Crates中使用 (33 直接)

ISC 许可证

28KB
571

Documentation Windows build status

coarsetime

一个Rust库,用于进行时间测量,侧重于速度。

此库是标准库中 TimeDuration 结构的局部替代品,具有以下区别

  • 速度优先于准确性。特别是,在Linux系统上使用 CLOCK_MONOTONIC_COARSE 来获取时钟值,并且转换避免了在非Intel系统上可能较慢的操作。
  • 可以将系统调用次数保持在最低。始终将“最近的时间戳”保存在内存中。它可以仅通过加载操作读取,并且可以按需要更新的频率进行更新。

安装

coarsetimecrates.io 上可用,并在Rust稳定版、beta版和nightly版上运行。

支持Windows和类Unix系统。

可用功能

  • wasi-abi2:当针对WASI时,使用ABИ的第二预览版。默认使用常规WASI-core ABI。

文档

API文档

示例

extern crate coarsetime;

use coarsetime::{Duration, Instant, Updater};

// Get the current instant. This may require a system call, but it may also
// be faster than the stdlib equivalent.
let now = Instant::now();

// Get the latest known instant. This operation is super fast.
// In this case, the value will be identical to `now`, because we haven't
// updated the latest known instant yet.
let ts1 = Instant::recent();

// Update the latest known instant. This may require a system call.
// Note that a call to `Instant::now()` also updates the stored instant.
Instant::update();

// Now, we may get a different instant. This call is also super fast.
let ts2 = Instant::recent();

// Compute the time elapsed between ts2 and ts1.
let elapsed_ts2_ts1 = ts2.duration_since(ts1);

// Operations such as `+` and `-` between `Instant` and `Duration` are also
// available.
let elapsed_ts2_ts1 = ts2 - ts1;

// Returns the time elapsed since ts1.
// This retrieves the actual current time, and may require a system call.
let elapsed_since_ts1 = ts1.elapsed();

// Returns the approximate time elapsed since ts1.
// This uses the latest known instant, and is super fast.
let elapsed_since_recent = ts1.elapsed_since_recent();

// Instant::update() should be called periodically, for example using an
// event loop. Alternatively, the crate provides an easy way to spawn a
// background task that will periodically update the latest known instant.
// Here, the update will happen every 250ms.
let updater = Updater::new(250).start().unwrap();

// From now on, Instant::recent() will always return an approximation of the
// current instant.
let ts3 = Instant::recent();

// Stop the task.
updater.stop().unwrap();

// Returns the elapsed time since the UNIX epoch
let unix_timestamp = Clock::now_since_epoch();

// Returns an approximation of the elapsed time since the UNIX epoch, based on
// the latest time update
let unix_timestamp_approx = Clock::recent_since_epoch();

依赖项

~0–550KB
~10K SLoC