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

hi monks, i want to find out the time taken for a perl program to execute.i want to print the time taken by the program to give the results, along with the results. any clues how to go about it?
  • Comment on how to find out the time taken for a perl program to execute ?

Replies are listed 'Best First'.
Re: how to find out the time taken for a perl program to execute ?
by blue_cowdawg (Monsignor) on Oct 11, 2006 at 18:08 UTC
        any clues how to go about it?

    I seem to be asking this follow up question a lot today: what platform are you referring to?

    One way to do this on a Unix or Unix like platform you can

    xtime myScript.pl
    where myScript.pl is the script you are producing metrics for.

    Another way you can do it that is rather crude is at the start of your script record the value of time() and at the end of the script do it again and check the difference. I've never used it, but Time::HiRes is a module you could look at for more fine toothed timing.


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
Re: how to find out the time taken for a perl program to execute ?
by madbombX (Hermit) on Oct 11, 2006 at 18:21 UTC
    There are a number of ways depending on how much information you want to make use of. On a *nix system, you can run your script with the 'time' command:

    time perl myscript.pl

    You could also benchmark your script with the Benchmark CPAN module.

    Another route would be code performance profiling. You can check out this thread that discusses it further: performance profiling.

Re: how to find out the time taken for a perl program to execute ?
by lyklev (Pilgrim) on Oct 11, 2006 at 19:48 UTC
    You can use the time function which will give you the system time in seconds. For longer programs this might do, but for very quick programs, you need the Time::Hires module, as blue_cowdawd mentioned.

    The perl documentation will tell you how the functions work. Homework for tonight: finding your way through the perl documentation.