for(my $i = 0; $i < @ARGVl $i++) {
if($ARGV[$i] =~ /meets some condition/) {
# ack! the index has been offset
$i += 2;
}
}
####
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;
}
}
####
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% --