use strict;
use warnings;
use Benchmark;
my @strings = qw(exception:tex exception:mex asdf);
Benchmark::cmpthese( -5, {
'one' => sub { my @filtered = grep { /exception:(?!tex)/} @strings
+; },
'two' => sub { my @filtered = grep { /exception/ && !/tex/ } @stri
+ngs; },
});
__END__
Rate one two
one 175458/s -- -15%
two 207611/s 18% --
update: The filter expressions are different. The second one does not care whether tex is before of after exception and doesn't require a ':' after 'exception'. I have stumbled on a patch of my ignorance trying to make the second match the same strings as the first. The best I have so far is:
use strict;
use warnings;
use Benchmark;
my @strings = qw(exception:tex exception:mex asdf tex:exception:mex);
Benchmark::cmpthese( -5, {
'one' => sub { my @filtered = grep { /exception:(?!tex)/} @strings
+; },
'two' => sub { my @filtered = grep { /exception/ && !/tex/ } @stri
+ngs; },
'three' => sub { my @filtered = grep { pos = 0; /exception:/g && !
+/\Gtex/ } @strings; },
});
__END__
Rate three one two
three 99554/s -- -18% -38%
one 121692/s 22% -- -24%
two 161067/s 62% 32% --
update: see also strange behavior of grep with global match [resolved]. Adding "pos = 0" is necessary to clear pos between iterations but it would not be necessary if there were only a single iteration. This makes it difficult to compare the efficiency of "/exception/g && !/\Gtex/". |