in reply to Elapsed time in perl

The way you've shown is right. However, you may want to make a function from it, if you don't want to clutter the code with time calls.

Either do this:

use warnings; use strict; sub measure_time(&) { my($btime, $etime); $btime = time; &{$_[0]}(); $etime = time; warn "elapsed time was: ", $etime - $btime, " s\n"; } measure_time { sleep 60; }; __END__

Or, if you want to measure time in such a way that it doesn't line up with scopes, then do this:

use warnings; use strict; { my @btime; sub BEGIN_TIME { push @btime, time; } sub END_TIME { @btime or die "error: END_TIME without BEGIN_TIME"; my($btime, $etime) = (pop @btime, time); warn "elapsed time was ", $etime - $btime, " s\n"; } END { @btime and warn "warning: BEGIN_TIME without END_TIME"; } } sub foo { sleep 2; END_TIME; sleep 2; } BEGIN_TIME; sleep 3; foo(); __END__

Also see the timethis function in the Benchmark module. If you want to time the whole script, not just some parts of it, and you're on a unix-like system, use the time shell builtin or the time(1) program.