Here's a module I made that gets a lot of use. It times the total runtime of a perl script and prints the runtime and script name at the terminal. The only thing needed to make it time a script is to put "use Timer::Runtime" into the script.

I'd like to request some peer review and ask for any suggestions.

The output if this is used on 'myscript.pl' would look like:
myscript.pl Started: Thu Aug 12 20:34:49 2010 myscript.pl Finished: Thu Aug 12 20:34:49 2010, time = 00:00:00.000114

Here is what the module looks like:
package Timer::Runtime; use strict; use Time::Elapse; Time::Elapse->lapse( my $now); BEGIN { print "$0 Started: " . ( scalar localtime ) . "\n"; } END { print "$0 Finished: " . ( scalar localtime ) . ", time = $now\n"; } 1; =head1 NAME Timer::Runtime =head1 SYNOPSIS Wrap a script up with only a use statement to denote its start, stop, +and duration times to STDOUT. =head1 DESCRIPTION =over use Timer::Runtime; # output <script_name> Started: Thu Aug 12 20:34:49 2010 <script_name> Finished: Thu Aug 12 20:34:49 2010, time = 00:00:00. +000114 =back =head1 Author awohld =cut

Replies are listed 'Best First'.
Re: Timer::Runtime proposed
by Anonymous Monk on Mar 28, 2011 at 03:32 UTC
    I would check $^T, then I would patch Devel::Timer by studying Devel::Modlist, so that
    perl -d:Timer myProgram.pl
    works the way you have use Timer::Runtime;, and I'd also make sure
    use Devel::Timer -startEnd;
    works the same way , so as not to interfere with Devel::Timers existing functionality.

    I hope you understand what I'm trying to say.

      I like your suggestion. I'm going to look into Devel::Timer and not creating a new module form scratch.

      Thanks
Re: Timer::Runtime proposed
by JavaFan (Canon) on Mar 28, 2011 at 12:29 UTC
    Due to the use of END, the program will not display a parting message on either a serious error (preventing END blocks to be run), or when doing an exec (which doesn't run END blocks).

    Personally, I prefer the time(1) utility, which doesn't suffer from the problems mentioned above, shows more detailed output (defaults to real/user/sys times, but can be show (much) more, specially GNU time), and doesn't actually change the program run.

      The END block error with 'exec' is pretty serious and explains some of the problems I've been having. Looks like the implementation of my module is not a very good idea.
      What if in the START block I started a OO timer and then in it's destroy method, calculated the stop time and printed it?

      I think that would get around the END problems.
        Did you try whether it works? Seems easier than asking here whether it does.
        $ perl -wE 'sub DESTROY {say "ping"} my $x = bless [];' ping $ perl -wE 'sub DESTROY {say "ping"} my $x = bless []; exec "echo"' $
        Alternatively, one could have consulted the manual page:
        Note that "exec" will not call your "END" blocks, nor will it invoke "DESTROY" methods on your objects.