Get time in milliseconds without an installing an extra package?

百般思念 提交于 2019-11-27 16:15:06

问题


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

I am running Linux.


回答1:


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;


来源:https://stackoverflow.com/questions/7997819/get-time-in-milliseconds-without-an-installing-an-extra-package

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!