in reply to for ( ; ; ) vs for ( .. )
Also you can't accidentally offset the index within the iterative code e.g
Compared tofor(my $i = 0; $i < @ARGVl $i++) { if($ARGV[$i] =~ /meets some condition/) { # ack! the index has been offset $i += 2; } }
This works both ways however as you may want to intentionally affect the offset, but you're possibly better off using a while loop in the first place.for my $i (0 .. $#ARGV) { if($ARGV[$i] =~ /meets some condition/) { # doesn't matter as $i will be set again in the next iteration $i += 2; } }
Here's a quick benchmark for those in need of speed
use Benchmark qw(cmpthese); my @list = 1..10000; my $r; cmpthese(-10, { cstyle => sub { for(my $i = 0; $i < @list; $i++) { $r = $list[$i]; } }, perlstyle => sub { for my $i (@list) { $r = $i; } } }); __output__ Benchmark: running cstyle, perlstyle, each for at least 10 CPU seconds +... cstyle: 15 wallclock secs (10.57 usr + 0.05 sys = 10.62 CPU) @ 36 +.06/s (n=383) perlstyle: 15 wallclock secs (10.10 usr + 0.03 sys = 10.13 CPU) @ 86 +.77/s (n=879) Rate cstyle perlstyle cstyle 36.1/s -- -58% perlstyle 86.8/s 141% --
_________
broquaint
update: added benchmark because it's just so darned easy
|
|---|