gpt4 book ai didi

perl - 如何在 Perl 中以纳秒为单位获取系统时间?

转载 作者:行者123 更新时间:2023-12-01 00:06:41 26 4
gpt4 key购买 nike

我想在 Perl 中以纳秒为单位获得系统时间。我试过Time::HiRes模块,它只支持微秒。

最佳答案

Time::HiRes模块最多支持微秒。正如@MarcoS 在当今常见的硬件中所回答的那样,使用软件计算的纳秒精度是无稽之谈。

随后的两个调用,获取当前微秒并在之后打印

perl -MTime::HiRes=time -E '$t1=time; $t2=time; printf "%.6f\n", $_ for($t1, $t2)'

结果(在我的系统上)
1411630025.846065
1411630025.846069

例如只获得两次当前时间,并且没有任何成本 3-4 微秒。

如果您想要一些“纳秒数”,只需以 9digit 精度打印时间,例如:
perl -MTime::HiRes=time -E '$t1=time;$t2=time; printf "%.9f\n", $_ for($t1, $t2)'

你会得到:
1411630910.582282066
1411630910.582283974

相当纳秒的时间;)

无论如何,您可以以合理的纳秒精度休眠。从文档

nanosleep ( $nanoseconds )

Sleeps for the number of nanoseconds (1e9ths of a second) specified. Returns the number of nanoseconds actually slept (accurate only to microseconds, the nearest thousand of them).

...

Do not expect nanosleep() to be exact down to one nanosecond. Getting even accuracy of one thousand nanoseconds is good.

关于perl - 如何在 Perl 中以纳秒为单位获取系统时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26032541/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com