I’ve done a small experiment as will be shown below and it looks like that a while loop is faster than a for loop in Perl. But since the experiment was rather crude, and the subject might be a lot more complicated than it seems, I’d like to hear what you have to say about this.
Thanks as always for any comments/suggestions 🙂
In the following two small scripts, I’ve tried while and for loops separately to calculate the factorial of 100,000. The one that has the while loop took 57 minutes 17 seconds to finish while the for loop equivalent took 1 hour 7 minutes 54 seconds.
Script that has while loop:
use strict;
use warnings;
use bigint;
my $now = time;
my $n = shift;
my $s = 1;
while(1){
$s *= $n;
$n--;
last if $n==2;
}
print $s*$n;
$now = time - $now;
printf("\n\nTotal running time: %02d:%02d:%02d\n\n", int($now / 3600),
int(($now % 3600) / 60), int($now % 60));
Script that has for loop:
use strict;
use warnings;
use bigint;
my $now = time;
my $n =shift;
my $s=1;
for (my $i=2; $i<=$n;$i++) {
$s = $s*$i;
}
print $s;
$now = time - $now;
printf("\n\nTotal running time: %02d:%02d:%02d\n\n", int($now / 3600),
int(($now % 3600) / 60), int($now % 60));
The loops are not equivalent, and you are primarily thrashing the bigint package and it has nothing to do with
forvswhileper se.The while loop uses the notation ‘
$s *= $i‘ but the for loop uses ‘$s = $s * $i‘.It is simple enough to demonstrate that these are not identical. Also, one loop counts up; the other counts down. This affects how big the numbers to be multiplied are. It is a second order effect – but not completely negligible.
[Update: revised to show just one version of the code, with sub-second timings. There is room to think that the printing should be excluded from the timing calculations; that makes things messier though, so I haven’t bothered. I have fixed the bug in the previous version: loop 4 was the same as loop 3 – now it isn’t. I’ve also tarted up the output formatting (though the sub-second handling could be improved – an exercise for the reader), and there is better ‘progress reporting’.]
The timing results on a Mac Mini (Snow Leopard 10.6.2) were:
The script:
And here’s a much more compact version of the code above, extended to test ‘while’ loops as well as ‘for’ loops. It also deals with most of the timing issues. The only thing that isn’t ideal (to me) is that it uses a couple of global variables, and I scrunched the code in the code refs slightly so it all fits on one line without triggering a scroll bar (on my display, anyway). Clearly, with a bit more work, the testing could be wrapped up into an array, so that the testing would be done iteratively – a loop through the array running the timer function on the information in the array. Etc…it’s a SMOP – Simple Matter of Programming. (It prints the MD5 hash of the factorial, rather than the factorial itself, because it is easier to compare the results, etc. It did point out a couple of errors as I was refactoring the code above. Yes, MD5 is not secure – but I’m not using it for security; just to spot unintentional changes.)
Example output (MD5 checksum compressed to avoid line breaking – the full value is
584b3ab832577fd1390970043efc0ec8):I consistently see a small (<1%) penalty for the ‘while’ loop over the corresponding ‘for’ loop, but I don’t have good explanation for it.