std::time::Duration是否与time::precision“time”from”一样精确;时间“;大木箱

std::time::Duration是否与time::precision“time”from”一样精确;时间“;大木箱,time,rust,Time,Rust,长期以来,精确测量锈蚀时间的标准方法是time板条箱及其功能。然而,time板条箱现在已经被弃用,并且std库已经打算用来测量经过的时间 我不确定它是否有同样的精度,至少在设计上是如此。我知道这可能是一个模糊的问题,因为这两种东西在不同的操作系统中有不同的实现,在不同的版本中实现可能会发生变化,但至少它们有相同的目的吗?至少从设计的角度来看,std::time::Duration是否正确地替代了time::precisive\u time\n 在我的系统(Mac OS)上运行此脚本会产生非常小的

长期以来,精确测量锈蚀时间的标准方法是
time
板条箱及其功能。然而,
time
板条箱现在已经被弃用,并且
std
库已经打算用来测量经过的时间

我不确定它是否有同样的精度,至少在设计上是如此。我知道这可能是一个模糊的问题,因为这两种东西在不同的操作系统中有不同的实现,在不同的版本中实现可能会发生变化,但至少它们有相同的目的吗?至少从设计的角度来看,
std::time::Duration
是否正确地替代了
time::precisive\u time\n

在我的系统(Mac OS)上运行此脚本会产生非常小的持续时间,因此它可能非常精确:

使用std::time::Instant;
fn main(){
让mut t=Instant::now();
环路{
println!(“{:?}”,t.appeased());
t=即时::现在();
}
}

是的,非常确定,
std::time::Instant
time::precise\u time\ns
的正确替代品,具有相同或更好的精度

从Rust 1.33.0开始,
time
0.1.41,大多数操作系统的
time::precise\u time\u ns()
std::time::Instant::now()
的实现是相同的,只有少数例外

  • Unix:相同,
    clock\u gettime(clock\u单调,…)
  • MacOS:相同,
    mach\u绝对时间
  • Windows:相同,
    QueryPerformanceCounter
  • Wasm32
    time
    没有实现,
    std
    使用
    TimeSysCall::perform(TimeClock::Monotonic)
  • 氧化还原:相同,与unix相同
  • SGX:实现方式不同,可能
    std
    的实现方式更正确
在未来版本中,
std::time::Instant::now
的实现不太可能恶化

实施细节 ,带有cfg标志,标准库具有每个系统的目录,带有(unix还具有针对mac os的条件编译,位于
time.rs

Unix,“常规”,而不是MacOS或iOS 两种实现都使用
clock\u gettime(3)
clock\u单调
clock\u id

时间
#[cfg(全部(非(target_os=“macos”)、非(target_os=“ios”))]

性病
#[cfg(unix)]
+
#[cfg(非任何(target_os=“macos”,target_os=“ios”)))]

Unix、MacOS或iOS 两种实现都使用
马赫绝对时间

顺便说一句,标准的
clock\u gettime(clock\u MONOTONIC,…)
也适用于我的系统,Mac OS 10.13.6,但我不确定它是否真的单调

时间
#[cfg(任意(target_os=“macos”,target_os=“ios”))]

性病
#[cfg(unix)]
+
#[cfg(任意(target_os=“macos”,target_os=“ios”))]

窗户 两种实现都使用

时间
#[cfg(windows)]

性病
#[cfg(windows)]

Wasm32 它可能是非网络使用的,与网络无关。它
time
crater它还没有实现

时间
#[cfg(全部(target_arch=“wasm32”,而不是(target_os=“emscripten”))]

性病
#[cfg(target_arch=“wasm32”)]

氧化还原 这两种实现都使用
clock\u gettime(clock\u MONOTONIC,…)
,与unux相同

时间
#[cfg(target_os=“redox”)]

性病
#[cfg(target_os=“redox”)]

新加坡元 这里的实现有所不同<代码>时间板条箱返回到std,并使用非单调时间(当时std中可能没有单调时间)。从time迁移到std可能会提高准确性,因为它使用特定于SGX的调用

时间
#[cfg(target_env=“sgx”)]

性病
#[cfg(全部(target_vendor=“fortanix”,target_env=“sgx”))]


至少在Linux上,使用
clock\u gettime(clock\u MONOTONIC)
,这样它们应该给出相同的结果。实现是特定于平台的,因此您可能需要自己比较相关平台的代码。Rust标准库的一般代码质量很高,因此我的基本假设是,
std::time
中的实现尽可能精确,因为目标平台允许。看起来文档可以改进。它应该说明保证的精度,或者明确解释没有提供任何保证。有。
40ns
42ns
41ns
45ns
40ns
41ns
40ns
40ns
41ns
41ns
41ns
40ns
40ns
40ns
let mut ts = libc::timespec { tv_sec: 0, tv_nsec: 0 };
unsafe {
    libc::clock_gettime(libc::CLOCK_MONOTONIC, &mut ts);
}
(ts.tv_sec as u64) * 1000000000 + (ts.tv_nsec as u64)
Instant { t: now(libc::CLOCK_MONOTONIC) }
unsafe {
    let time = libc::mach_absolute_time();
    let info = info();
    time * info.numer as u64 / info.denom as u64
}
Instant { t: unsafe { libc::mach_absolute_time() } }
let mut ticks = i64_to_large_integer(0);
unsafe {
    assert!(QueryPerformanceCounter(&mut ticks) == 1);
}
mul_div_i64(large_integer_to_i64(ticks), 1000000000, frequency()) as u64
let mut t = Instant { t: 0 };
cvt(unsafe {
    c::QueryPerformanceCounter(&mut t.t)
}).unwrap();
t
unimplemented!()
Instant(TimeSysCall::perform(TimeClock::Monotonic))
let mut ts = syscall::TimeSpec { tv_sec: 0, tv_nsec: 0 };
syscall::clock_gettime(syscall::CLOCK_MONOTONIC, &mut ts).unwrap();
(ts.tv_sec as u64) * 1000000000 + (ts.tv_nsec as u64)
Instant { t: now(syscall::CLOCK_MONOTONIC) }
// This unwrap is safe because current time is well ahead of UNIX_EPOCH, unless system clock is adjusted backward.
let std_duration = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap();
std_duration.as_secs() * NANOS_PER_SEC + std_duration.subsec_nanos() as u64
Instant(usercalls::insecure_time())