无需安装额外的软件包即可获得以毫秒为单位的时间?

发布于 2024-12-13 12:36:25 字数 61 浏览 5 评论 0原文

如何在 Perl 中获取以毫秒为单位的时间而不安装任何额外的软件包?

我正在运行Linux。

How can I get the time in milliseconds in Perl without installing any extra package?

I am running Linux.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

友欢 2024-12-20 12:36:25

Time::HiRes 自 Perl 5.7.3 以来一直是核心的一部分。要检查其可用性,请检查 Perl 版本 perl -v,或尝试将其与 perl -e 'use Time::HiRes;' 一起使用,两者均来自命令行。

示例用法:

use Time::HiRes qw/ time sleep /;

my $start = time;
sleep rand(10)/3;
my $end   = time;

print 'Slept for ', ( $end - $start ) , "\n";

要基于 Konerak 的注释进行构建,如果它不存在或无法使用,请通过反引号使用本机 Linux 命令:

sub time_since_epoch { return `date +%s.%N` }

print time_since_epoch;

Time::HiRes has been part of the core since Perl 5.7.3. To check for its availability, check for the Perl version, perl -v, or try to use it with perl -e 'use Time::HiRes;', both from the command line.

Sample usage:

use Time::HiRes qw/ time sleep /;

my $start = time;
sleep rand(10)/3;
my $end   = time;

print 'Slept for ', ( $end - $start ) , "\n";

To build on Konerak's comment, if it isn't there or it cannot be used, use native Linux commands via backticks:

sub time_since_epoch { return `date +%s.%N` }

print time_since_epoch;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文