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

I am a new user to Perl and I am writing a small program. It may take several hours to run. I wonder whether there is timing block in Perl, that similar the "time.h" in C program, to calculate how long this program runs. I have searched on internet, all the time modules like "local time" don't have such features. Thanks for your help!!

Replies are listed 'Best First'.
Re: Question about time module
by ikegami (Patriarch) on Aug 30, 2007 at 20:04 UTC
    my $start_time = time; END { my $exec_time = time - $start_time; print("Program took $exec_time seconds to execute\n"); } ...[ put rest of program here ]...

    or if you want to include compile time

    my $start_time; BEGIN { $start_time = time; } END { my $exec_time = time - $start_time; print("Program took $exec_time seconds to execute\n"); } ...[ put rest of program here ]...

    Neither will include the time it took to load Perl itself.
    Add use Time::HiRes qw( time ); if you want higher precision results.

Re: Question about time module
by mmmmtmmmm (Monk) on Aug 31, 2007 at 03:00 UTC
    You could also use Benchmark;

    From perldoc Benchmark:

    The Benchmark module encapsulates a number of routines to help you figure out how long it takes to execute some code.

    timethis - run a chunk of code several times
    timethese - run several chunks of code several times
    cmpthese - print results of timethese as a comparison chart
    timeit - run a chunk of code and see how long it goes
    countit - see how many times a chunk of code runs in a given time


    ----mmmmtmmmm
Re: Question about time module
by menolly (Hermit) on Aug 31, 2007 at 18:35 UTC
Re: Question about time module
by Perlbotics (Archbishop) on Sep 01, 2007 at 20:05 UTC
    times() might be of some use too..., maybe in combination with ikegamis proposal? (taken from: perldoc -f times)
    times   Returns a four-element list giving the user and system times, in seconds, for this process and the children of this process.
    
                       ($user,$system,$cuser,$csystem) = times;
    
                   In scalar context, "times" returns $user.