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 which provides gettimeofday() microsecond-resolution timer function.
To install Time::HiRes on Linux, run the following.
You can measure elapsed time in microsecond resolution 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]); # Or you can get second/microsecond components separately, by using: my ($seconds, $microseconds) = gettimeofday; // current time
Subscribe to Xmodulo
Do you want to receive Linux FAQs, detailed tutorials and tips published at Xmodulo? Enter your email address below, and we will deliver our Linux posts straight to your email box, for free. Delivery powered by Google Feedburner.
Did you find this tutorial helpful? Then please be generous and support Xmodulo!