in reply to Re: counting backward in thread counting backward
Which is okay for small ranges; but creates a huge list for large ones.
:) seems like an easy candidate for optimization , since this is optimized for (reverse @a) is optimized
perl -le " @f = 1 .. 100_000; for(reverse @f ){ $f=$_; $f==$#f and sca +lar<> } " perl -le " @f = 1 .. 100_000; for( @f ){ $f=$_; $f==$#f and scalar<> } + " [download]
But in for(reverse @f ) there is no for-context which helps reverse to act differently.
I'm to lazy for benchmarks (we had them before), but this was surely not fixed in 5.8.8
Cheers Rolf
Dave.
I had to stop the second version because the swapping started to freeze my system:
lanx@nc10-ubuntu:~$ perl -e'for my $i ( 0 .. 1e8 ) { $x*=-1; $x+=$i }; +print "$x\n"' 50000000 lanx@nc10-ubuntu:~$ perl -e'for my $i ( reverse 0 .. 1e8 ) { $x*=-1; $ +x+=$i };print "$x\n"' ^C^C^C lanx@nc10-ubuntu:~$ perl -version This is perl, v5.10.0 built for i486-linux-gnu-thread-multi ... [download]
Reversing ranges was what BUK did, but AnoMonk replied with a reference to a fix for reversing arrays!
But you still had to create a huge array in order to benefit from the optimisation; which kinda negates the purpose.
This: perl -E"@a=1..1e9; for( reverse @a ) { print $_; <> }" consumes 18GB (that's Gigabytes!) of memory
Whereas this: perl -E"for( -1e9..1 ) { print -$_; <> }" iterates the same range and consumes only 1.8MB of ram.
Four orders of magnitude more memory, or a minus sign. My choice is simple; how about yours?
I'll use minus sign until perl5-porter optimizes foreach reverse rangeop
Or i'll use for;;;
It's a shame you can't use the c-style for as a modifier:
cmpthese -1, { a=>q[ 1 for -1e6..0; ], b=>q[ for( my $i=1e6; $i; --$i ){ 1 } ] };; Rate b a b 14.0/s -- -26% a 19.0/s 35% -- [download]
Ceramics Glass Wood Metal Plastic Paper Banana leaves Something else
Results (446 votes), past polls