nisha has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks, I wanted to know if there is an easy way to calculate the elapsed time in perl. I am using the time function.
$sttime = time; sleep(60); $entime = time; $elapse = $entime - $sttime; print "Elapsed time : $elapse";
The output is in seconds; which needs further conversion bby dividing by 60 etc. Could you please help me find an easier way to calculate the elapsed time. Please help. Thanks, Nisha

Replies are listed 'Best First'.
Re: Elapsed time in perl
by McDarren (Abbot) on Dec 26, 2005 at 09:44 UTC
    This suggestion I gave a few days ago to a similar question might help.

    As far as converting elapsed seconds into a "human readable" format, have a look at this thread, which bought about several suggestions.

    Cheers,
    Darren :)

      Thank You very much. It really helped :). Thanks, Nisha
Re: Elapsed time in perl
by tirwhan (Abbot) on Dec 26, 2005 at 09:48 UTC

    Take a look at DateTime

    use DateTime; $sttime = DateTime->now(); sleep(61); $entime = DateTime->now; $elapse = $entime - $sttime; print "Elapsed time : ".$elapse->in_units('minutes')."m\n"; __OUTPUT__ Elapsed time : 1m

    A computer is a state machine. Threads are for people who can't program state machines. -- Alan Cox
Re: Elapsed time in perl
by ambrus (Abbot) on Dec 26, 2005 at 12:29 UTC

    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.

Re: Elapsed time in perl
by blazar (Canon) on Dec 27, 2005 at 13:46 UTC

    The way you use to calculate the elapsed time is correct. You can use any of the various modules for date/time calculations to nicely format it in a human readable way.

    Incidentally, almost by definitition, there's no need to create unnecessary temporary variable that are used once only:

    my $sttime = time; sleep(60); print "Elapsed time : ", time - $sttime;
    Now, that is "simpler" - I also added a my, since you're use'ing strict anyway, don't you?
    ;-)