Optimisation
Chris Jack
chris_jack at msn.com
Tue Mar 3 16:25:47 GMT 2009
> On Mon, Mar 2, 2009 at 9:01 PM, Nigel Peck <nigel.peck at miswebdesign.com> wrote:
> >
> > When the list could be empty, which is faster?
> >
> > if ( @list ) { foreach ( @list ) { } }
> >
> > - or just -
> >
> > foreach ( @list ) { }
> >
> > Or is it a pointless question?
The chances that the formatting up in the following stuffs up from hotmail's html being converted to text are high. If I run the following:
my @list;
my $k;
my $i = 0;
while ($i < 10000000) {
if (@list) {
for $k (@list) {
print $k;
}
}
$i++;
}
it takes 8.315 seconds. If I remove the "if (@list)" and rerun it, it takes 11.781 seconds. If I rerun the same two tests but with one element in the list the timings go to 25.337 seconds and 21.704 seconds respectively - so you would only expect an improvement if over 50% of your cases had an empty list.
But I agree with most of the other comments. I had to put the loop up to 10 million to get meaningful timings and life is to short to save 3 seconds on 10 million interations - and it's much clearer and readable without the "if".
Chris
_________________________________________________________________
25GB of FREE Online Storage – Find out more
http://clk.atdmt.com/UKM/go/134665320/direct/01/
More information about the london.pm
mailing list