- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
长期以来,在 Rust 中精确测量时间的标准方法是 time
crate 及其 time::precise_time_ns
功能。但是,time
crate 现在已被弃用,std
库有 std::time::Instant
用于测量耗时。
我不确定它是否具有相同的精度,至少在设计上是这样。我知道这可能是一个模糊的问题,因为对于不同的操作系统,这两种东西都有不同的实现,并且实现可能会在不同版本中发生变化,但至少它们是否具有相同的目的? std::time::Duration
是否是 time::precise_time_ns
的正确替代品,至少从其设计的角度来看?
在我的系统 (Mac OS) 上运行这个脚本输出的持续时间非常短,所以它可能非常精确:
use std::time::Instant;
fn main() {
let mut t = Instant::now();
loop {
println!("{:?}", t.elapsed());
t = Instant::now();
}
}
40ns
42ns
41ns
45ns
40ns
41ns
40ns
40ns
41ns
41ns
41ns
40ns
40ns
40ns
最佳答案
是的,非常确定,std::time::Instant
是 time::precise_time_ns
的正确替代品,具有相同或更好的精度。
从 Rust 1.33.0 开始,time
0.1.41,time::precise_time_ns()
和 std::time::的大多数操作系统的实现: Instant::now()
是一样的,只有少数异常(exception)。
clock_gettime(CLOCK_MONOTONIC, ...)
mach_absolute_time
QueryPerformanceCounter
time
没有实现,std
使用 TimeSysCall::perform(TimeClock::Monotonic)
std
有更正确的实现在未来的版本中 std::time::Instant::now
实现不太可能会恶化。
time
crate has all implementations in single file ,带有 cfg 标志,标准库有每个系统的目录,带有 mod.rs where implementation is chosen at compile time (unix在time.rs
里面也有针对mac os的条件编译)。
两种实现都使用 clock_gettime (3)
和 CLOCK_MONOTONIC
clock_id
。
#[cfg(all(not(target_os = "macos"), not(target_os = "ios")))]
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)
#[cfg(unix)]
+ #[cfg(not(any(target_os = "macos", target_os = "ios")))]
Instant { t: now(libc::CLOCK_MONOTONIC) }
两种实现都使用 mach_absolute_time
。
顺便说一句,标准 clock_gettime(CLOCK_MONOTONIC, ...)
也适用于我的系统,Mac OS 10.13.6,但我不确定它是否真的单调。
#[cfg(any(target_os = "macos", target_os = "ios"))]
unsafe {
let time = libc::mach_absolute_time();
let info = info();
time * info.numer as u64 / info.denom as u64
}
#[cfg(unix)]
+ #[cfg(any(target_os = "macos", target_os = "ios"))]
Instant { t: unsafe { libc::mach_absolute_time() } }
两种实现都使用 QueryPerformanceCounter
#[cfg(windows)]
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
#[cfg(windows)]
let mut t = Instant { t: 0 };
cvt(unsafe {
c::QueryPerformanceCounter(&mut t.t)
}).unwrap();
t
它可能用于非网络用途,与 web-sys 无关.它时间
将它未实现。
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
unimplemented!()
#[cfg(target_arch = "wasm32")]
Instant(TimeSysCall::perform(TimeClock::Monotonic))
两种实现都使用 clock_gettime(CLOCK_MONOTONIC, ...)
,与 unux 相同。
#[cfg(target_os = "redox")]
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)
#[cfg(target_os = "redox")]
Instant { t: now(syscall::CLOCK_MONOTONIC) }
这里的实现不同。 time
crate 回落到 std,并使用非单调时间(当时 std 中可能没有单调时间)。可能不时迁移到 std 会提高准确性,因为它使用 SGX 特定的调用。
#[cfg(target_env = "sgx")]
// 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
#[cfg(all(target_vendor = "fortanix", target_env = "sgx"))]
Instant(usercalls::insecure_time())
关于time - std::time::Duration 是否与 "time" crate 中的 time::precise_time_ns 一样精确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55583503/
感觉我在这里遗漏了一些明显的东西,所以提前道歉。无论如何,这是我尝试转换的一些数据a: acct_num year_prem prem exc 001 20
我正在尝试将表中的模式与用户话语 匹配。 string userUtterance = "I want identification number for number of customers";
当尝试在 Precise 上链接 gccgo 时,出现此链接错误: matt@matt-1005P:~/src/gopath/src/meme$ gccgo cmd/meme/main.go -o m
假设我有以下数据和命令: clc;clear; t = [0:0.1:1]; t_new = [0:0.01:1]; y = [1,2,1,3,2,2,4,5,6,1,0]; p = interp1(
假设我有以下数据和命令: clc;clear; t = [0:0.1:1]; t_new = [0:0.01:1]; y = [1,2,1,3,2,2,4,5,6,1,0]; p = interp1(
我总是想给精确匹配比只匹配前缀的分数更高的分数(例如,“ball”在与“ball*”匹配时应该比“ballistic”得到更高的分数)。 我当前(详细)的方法是在创建 PrefixQuery 时始终执
有什么解决方法可以让我在 Android 中使用 long 或 double 来寻找音频文件中的位置吗?目前 seekTo 只接受 ints 参数。我想更精确(比如在十分之一秒内) int resID
我的 replacingOccurrences 函数有问题。我有一个这样的字符串: let x = "john, johnny, johnney" 我需要做的只是删除“john” 所以我有这段代码:
我正在使用 BeautifulSoup 进行网页抓取。我有这段代码来提取 a 标签的值,但它似乎不起作用。显示错误: AttributeError: 'int' object has no attri
我要在带有标记顶点和标记有向边的图上寻找一种不精确的图匹配算法。我的任务是检测两个图表的变化以将它们显示给开发人员(想想颠覆差异)。我已经实现了基于禁忌搜索 ( this ) 的优化算法,但我无法让该
我有两个网站: example.com 和 yyy.com 他们都有类似的网络应用程序,但在不同的服务器上。我想让 Apache 将所有路径请求重定向到 example.com 与 完全相同的方式yy
因此,我尝试合并两个公司信息数据库(从现在起表 A 和表 B),其中最常见(且可靠)的单一引用点是网站 URL。表 A 已更新,表 B 待更新。 我已经从表 A 中提取了 URL,并使用 PHP 清理
我正在 http://classicorthodoxbible.com/new.html 上制作效果主要描述中的 Angular 色,包裹在自己的跨度中,从他们通常的休息地点移动到随机位置,然后通过指
我目前正在使用我的 Raspberry Pi 及其内置 UART 输入编写 MIDI 合成器。 在某个时间点,为了启用 MIDI 输入的实时回放,我必须设置一种环形缓冲区以与 OpenAL 一起使用,
在 C 中,当设置了一个 float 时, int main(int argc, char *argv[]) { float temp = 98.6f; printf("%f\n",
实现 MP3 无间隙循环的最佳可能性是什么?目前我正在使用 AVAudioPlayer 并将 .numberOfLoops() 属性设置为 -1 但可以听到,轨道重新启动。情况并非如此,例如使用 Tr
我想创建不一定是“正确”矩阵的“类矩阵”对象。但是,确切地说,“类矩阵”是什么意思? 示例 1 > image(1:9) Error in image.default(1:9) : argument
给定一个像这样的 XML 文档: john &title; 我想解析上面的 XML 文档并生成其所有实体已解析的副本。因此,给定上述 XMl 文档,解析器应输出: john
需要说明的是,这种方法不是我要找的: 事实上,此方法会调整 ImageField 的大小。我想将 Image 对象的大小调整为特定且精确的无比例分辨率。有什么办法吗? --编辑-- 对我来说,Ima
我正在尝试使用 TF2.0 eager 模式执行精确的 GP 回归,基于来自 https://colab.research.google.com/github/tensorflow/probabili
我是一名优秀的程序员,十分优秀!