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

I've been trying to create a module for timing the runtime of my scripts. Well, I had some working code that I am trying to transplant to a module.

This is what I have:

package Time::SinceStart; use strict; use Time::HiRes qw(gettimeofday); require Exporter; our @ISA = qw(Exporter); our @EXPORT = qw(getTimeSinceStart); our @EXPORT_OK = qw(getTimeSinceStart); our $VERSION = 1.0; my @pagestarttime = (); sub BEGIN { @pagestarttime = gettimeofday; } sub getTimeSinceStart { my @pagestoptime = gettimeofday; my $seconds = (abs($pagestoptime[0]-$pagestarttime[0])); my $decimals = (abs($pagestoptime[1]-$pagestarttime[1])); return ($seconds, $decimals); } 1;

The problem is, I get:

null: Use of uninitialized value in subtraction (-) at /Library/Perl/T +ime/SinceStart.pm line 24. null: Use of uninitialized value in subtraction (-) at /Library/Perl/T +ime/SinceStart.pm line 25.

Lines 24 & 25 are the my $seconds & my $decimals lines.

Now, there probably is a module for this (though I could not find it), but regardless, I'd also like to know what I do wrong. I'm guessing it has to do with global variables (oh, and this is run under mod_perl).

Replies are listed 'Best First'.
Re: Finding out how long a script has been running
by japhy (Canon) on Jun 21, 2002 at 16:10 UTC
    Remove the = () from your my @pagestarttime = (); line! That little bit is happening at run-time, after the BEGIN block which happened at compile-time!

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a job (NYC-area)
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

      Ergh.

      /me gets that sinking feeling he shoulda known that.

      Oh well, I'm still new with them fancy BEGIN END things. Thanks! =)

Re: Finding out how long a script has been running
by Abigail-II (Bishop) on Jun 21, 2002 at 16:31 UTC
    By just subtracting start and stop times, all you get is time passed between the beginning and the end of your script. That's not the time it took for your program to run - your program has to compute with other programs for the CPU. You'd better off using the times function - or you would just use the Benchmark module which is part of the standard distribution.

    Abigail

      Thanks for the suggestion, but I realize my question was badly phrased. I really did want to find out the time it took from start to finish, since it's used to put the time in the comment of a webpage, just for the fun of it...
Re: Finding out how long a script has been running
by little (Curate) on Jun 21, 2002 at 16:55 UTC

    I suggest to look up perlman:perlvar of which I here cite:

    $BASETIME

    $^T

    The time at which the program began running, in seconds since the epoch (beginning of 1970). The values returned by the -M, -A, and -C filetests are based on this value.

    So you might be looking for something alike this

    my $runTimeInSecs = time() - $^T;


    Have a nice day
    All decision is left to your taste
Re: Finding out how long a script has been running
by lshatzer (Friar) on Jun 21, 2002 at 16:52 UTC
    You could also use $^T, it is the epoch when the script started. Check perlvar out.
      If you go this route, be aware under mod_perl or other situations where you script is persistant, it might not be the number you want.

      -Lee

      "To be civilized is to deny one's nature."