zzztimbo zzztimbo - 4 years ago 243
Perl Question

Is there a better way to determine elapsed time in Perl?

my $start_time = [Time::HiRes::gettimeofday()];
my $diff = Time::HiRes::tv_interval($start_time);

print "\n\n$diff\n";

Answer Source

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::HiRes::gettimeofday();
my $end = Time::HiRes::gettimeofday();
printf("%.2f\n", $end - $start);


use Time::HiRes qw( time );

my $start = time();
my $end = time();
printf("%.2f\n", $end - $start);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download