在不安装额外软件包的情况下以毫秒为单位获取时间? [英] Get time in milliseconds without an installing an extra package?
本文介绍了在不安装额外软件包的情况下以毫秒为单位获取时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在不安装任何额外软件包的情况下在 Perl 中获取以毫秒为单位的时间?
How can I get the time in milliseconds in Perl without installing any extra package?
我正在运行 Linux.
I am running Linux.
推荐答案
Time::HiRes
自 Perl 5.7.3 以来一直是核心的一部分.要检查其可用性,请检查 Perl 版本,perl -v
,或尝试将其与 perl -e 'use Time::HiRes;'
一起使用,两者都来自命令行.
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.
示例用法:
use Time::HiRes qw/ time sleep /;
my $start = time;
sleep rand(10)/3;
my $end = time;
print 'Slept for ', ( $end - $start ) , "
";
要基于 Konerak 的评论,如果它不存在或无法使用,请通过反引号使用本机 Linux 命令:
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;
这篇关于在不安装额外软件包的情况下以毫秒为单位获取时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文