Get time in milliseconds without an installing an extra package?
25,447
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;
Author by
JJ Liu
Updated on June 16, 2020Comments
-
JJ Liu about 4 years
How can I get the time in milliseconds in Perl without installing any extra package?
I am running Linux.
-
JRFerguson over 12 yearsTo load and/or verify a module from the command line you can use: perl -MTime::HiRes -e 1 in lieu of: perl -e 'use Time::HiRes'
-
Zaid over 12 years@mob : Thanks for the correction. @James_R_Ferguson : I purposely left out the
-M
flag because I didn't want to confuse the questioner with the syntax (using1
instead of''
is a nifty little tip though :). -
JJ Liu over 12 years@Zaid thanks! is the value returned by calling time in milliseconds?
-
Zaid over 12 years@JJLiu : It's the time in seconds since Jan 1, 1970 (aka the epoch)