I thought that too, but it isn't faster:
use warnings;
use strict;
use Benchmark qw(:all);
open my $fh, '<', $ARGV[0] or die $!;
cmpthese(100, {
all => '_all',
while => '_while',
});
sub _all {
seek $fh, 0, 0;
my $count = 0;
while (<$fh>){
$count += () = /(M+)/g;
}
# print "all: $count\n";
}
sub _while {
seek $fh, 0, 0;
my $count=0;
while(<$fh>){
while($_=~/(M+)/g){
$count++;
}
}
# print "while: $count\n";
}
__END__
Rate all while
all 11.5/s -- -25%
while 15.2/s 33% --
|