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

If I set $time = localtime; at the top of the script and $endtime = localtime; at the bottom, all I would have to do is subtract $time from $endtime to see how long it took, right?

I just want an easy way to see how long it took to complete in terms of hours, minutes and seconds.

Replies are listed 'Best First'.
Re: time it took script to complete
by borisz (Canon) on Nov 07, 2004 at 19:18 UTC
    The $^T var contains the starttime of the script. So to compute the runtime, it is enough to do:
    time - $^T.
    Boris
Re: time it took script to complete
by TStanley (Canon) on Nov 07, 2004 at 18:51 UTC
    You can also use the Benchmark module to determine this. Or if you are running on a *nix system, the timex command may be of some use.

    TStanley
    --------
    The only thing necessary for the triumph of evil is for good men to do nothing -- Edmund Burke
Re: time it took script to complete
by bart (Canon) on Nov 07, 2004 at 19:59 UTC
    Use time, not localtime. You'll calculate the difference in seconds, that way.

    If you want a higher resolution than whole seconds, use

    use Time::HiRes 'time';
    first. It'll replace the built in time function.
Re: time it took script to complete
by thetimeboy (Novice) on Nov 07, 2004 at 18:57 UTC
    Actually if you just want to count the seconds (or minutes, hours etc) you are better off using time() which returns purely the seconds since 1970. From that its easier to get minutes and hours:

    my $t0 = time;
    sleep(61);
    my $t1 = time;
    my $seconds = $t1 - $t0;
    my $s = $seconds % 60;
    my $m = ($seconds - $s) / 60;
    print ("$m minutes and $s seconds\n");
    # etc
      I think my $m = int($seconds/60) is cleaner and more self documenting personally.....
Re: time it took script to complete
by terra incognita (Pilgrim) on Nov 07, 2004 at 19:12 UTC
    You can use "times" function to determine how many user and system seconds a process actually used. This will give a better measurement of the time that a process actually takes to complete, since other processes can slow the execution of your program down and skew your results. Execute this code just before exiting your script.
    ($user,$system,$cuser,$csystem) = times; print "user time = $user\n"; print "system time = $system\n"; print "children user time = $user\n"; print "children system time = $system\n";
Re: time it took script to complete
by tmoertel (Chaplain) on Nov 08, 2004 at 00:10 UTC
    If you're using a Unix-like system, one option is the system time command, which (unsurprisingly) times how long it takes for a command to execute. For example, to time how long it takes the wc command to process the system dictionary:
    $ time wc /usr/share/dict/words 45427 45427 409305 /usr/share/dict/words real 0m0.232s user 0m0.170s sys 0m0.010s
    You can use the same technique to time your scripts, provided that you can execute them from the command line.

    Cheers,
    Tom

Re: time it took script to complete
by perlcapt (Pilgrim) on Nov 07, 2004 at 21:18 UTC
Re: time it took script to complete
by TedPride (Priest) on Nov 08, 2004 at 04:22 UTC
    I just do:
    my $time = time(); ... print time() - $time;
    Usually with the part in the middle looping between 100,000 and 10,000,000 times so I can get an accurate reading of how much time is used per iteration. This method works fine for comparing algorithms, if not for returning the time it took the script to run.

    NOTE: Since different amounts of system resources may be allocated for different runs of the script, you have to time all algorithms inside the same run of the script to get an accurate comparison.

    Also see post 332827 for info on milliseconds.

Re: time it took script to complete
by radiantmatrix (Parson) on Nov 08, 2004 at 18:03 UTC

    I personally use Time::Stopwatch for this. If you use Time::HiRes as well, it will be more precise. The code is clean and easy to read:

    use Time::Stopwatch; tie my $timer, 'Time::Stopwatch'; #.. your code here print "Completed in $timer seconds";

    Of course, you can use some simple math to break the elapsed seconds into H:M:S format if you'd like.

    radiantmatrix
    require General::Disclaimer;
    "Users are evil. All users are evil. Do not trust them. Perl specifically offers the -T switch because it knows users are evil." - japhy