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.
| [reply] [d/l] [select] |
Re: time it took script to complete
by TStanley (Canon) on Nov 07, 2004 at 18:51 UTC
|
| [reply] |
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. | [reply] [d/l] |
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 | [reply] |
|
|
I think my $m = int($seconds/60) is cleaner and more self documenting personally.....
| [reply] [d/l] |
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";
| [reply] [d/l] |
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
| [reply] [d/l] |
Re: time it took script to complete
by perlcapt (Pilgrim) on Nov 07, 2004 at 21:18 UTC
|
Using $start = localtime(time()) returns a date-time string since it is being used in a scalar context. See localtime, gmtime, and particularly time. Better yet, read about them in Programming Perl.
| [reply] |
Re: time it took script to complete
by TedPride (Priest) on Nov 08, 2004 at 04:22 UTC
|
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. | [reply] [d/l] |
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
| [reply] [d/l] |