Possibly. Depends on what you mean by “better”.
If you are asking for a “better” solution in terms of functionality, then this is pretty much it.
If you are asking for a “better” in the sense of “less awkward” notation, then know that, in scalar context, Time::HiRes::gettimeofday()
will return floating seconds since epoch (with the fractional part representing microseconds), just like Time::HiRes::time()
(which is a good drop-in replacement for the standard time()
function.)
my $start_time = Time::HiRes::gettimeofday();
...
my $stop_time = Time::HiRes::gettimeofday();
printf("%.2f\n", $stop_time - $start_time);
or:
use Time::HiRes qw( time );
my $begin_time = time();
...
my $end_time = time();
printf("%.2f\n", $end_time - $begin_time);