Last updated on June 2, 2020 by Dan Nanni
In some cases, you may need to measure time in high-resolution, for example, microsecond resolution as provided by gettimeofday()
system call. In order to use microsecond-resolution time in Perl, you can use Time::HiRes
Perl module. This module provides microsecond-resolution "wall-clock" time.
Starting with Perl v5.8, Time::HiRes
module comes as standard. If your Linux system does not have Time::HiRes
, you can install it using CPAN as follows.
$ sudo perl -MCPAN -e ‘install Time::HiRes’
You can measure elapsed time in microsecond resolution with Time::HiRes
as follows.
#/usr/bin/perl use Time::HiRes qw( usleep ualarm gettimeofday tv_interval ); my $t0 = [gettimeofday]; [add some computation intensive routine] my $elapsed = tv_interval ($t0, [gettimeofday]);
Alternatively, you can get second/microsecond components separately as follows.
# get current time my ($seconds, $microseconds) = gettimeofday;
The Time::HighRes
module supports up to microsecond granularity. High-level scripted languages such as Perl do not provide nanosecond-resolution timestamps. For that, you will need different languages such as C/C++.
This website is made possible by minimal ads and your gracious donation via PayPal or credit card
Please note that this article is published by Xmodulo.com under a Creative Commons Attribution-ShareAlike 3.0 Unported License. If you would like to use the whole or any part of this article, you need to cite this web page at Xmodulo.com as the original source.
Xmodulo © 2021 ‒ About ‒ Write for Us ‒ Feed ‒ Powered by DigitalOcean