Linux 在不安装额外软件包的情况下以毫秒为单位获取时间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7997819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 06:59:45  来源:igfitidea点击:

Get time in milliseconds without an installing an extra package?

linuxperltimepackagemilliseconds

提问by JJ Liu

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

如何在不安装任何额外包的情况下在 Perl 中获得以毫秒为单位的时间?

I am running Linux.

我正在运行 Linux。

采纳答案by Zaid

Time::HiReshas 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.

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

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:

要建立在 Konerak 的评论之上,如果它不存在或无法使用,请通过反引号使用本机 Linux 命令:

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

print time_since_epoch;