That was an off the cuff remark about using for(;;) instead of for ($lower..$upper). Of course there are uses of for(;;) that do not map on to for ($i..$j). However this wasnt one of them. And for this type of case I stand by my remarks.
Furthermore comparing
tofor (my $i = 0; $i < 1000000; $i += 2) {$sum += $i}
is a bit strange, when internally it is closer tofor my $i (map {$_ * 2} 0 .. 1000000 / 2) {$sum += $i}
As for all the other languages you listed, none (that I know of) but the C derivatives have this weird form of while loop pretending to be a for loop. For a programmer whose experience is limited to one of the ones without the equivelence the construct is particularly confusing. So from that POV I stand by calling the three arg for loop construct a dirty C trick.{ my $i=0; while ($i < 1000000) { $sum+=$i; } continue { $i+=2; } }
And I stand by the efficiency claims for the case to which I was primarily referring:
Id say thats more efficient wouldnt you?use strict; use Benchmark qw(cmpthese); our $sum; cmpthese -10,{ 'foreach' => <<'CODE', 'for(;;)' => <<'MORECODE' }; $::sum=0; for my $i (0..10_000) { $::sum+=$i; } CODE $::sum=0; for (my $i=0;$i<10_000;$i++) { $::sum+=$i; } MORECODE __END__ Benchmark: running for(;;), foreach, each for at least 10 CPU seconds. +.. for(;;): 11 wallclock secs (10.61 usr + 0.00 sys = 10.61 CPU) @ 85 +.30/s (n=905) foreach: 11 wallclock secs (10.63 usr + 0.00 sys = 10.63 CPU) @ 14 +3.15/s (n=1521) Rate for(;;) foreach for(;;) 85.3/s -- -40% foreach 143/s 68% --
--- demerphq
my friends call me, usually because I'm late....
In reply to Re: Re: extracting corresponding values from another array
by demerphq
in thread extracting corresponding values from another array
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |