It seems that using the regular expression natively is very fast. This is followed by the regular exprssion and OR logic. The split is slowest.
use Benchmark qw (cmpthese); @data = ('foo 1000 bar 1000', 'foo 1000 bar 500', 'foo 500 bar 1000', 'foo 500 bar 500', 'foo 1 bar 1', 'foo 1 bar 2', 'foo 2 bar 2', 'foo 10000 bar 1000', 'foo 10000 bar 10000', ); my $tot = 5; my $count = 100000; cmpthese($count, { 'ref' => sub { my $x=0; for (@data) { $x++ if /^\S+ (\d+) \S+ \1$/; } die "$x" unless $x == $tot; }, 'split' => sub { my $x=0; for (@data) { my @arr = split /\s+/; $x++ if $arr[1] == $arr[3]; } die $x unless $x == $tot; }, 'simple' => sub { my $x=0; for (@data) { $x++ if !m/foo (\d+) bar (\d+)/ || $1 == $2; } die unless $x == $tot; }, }); __END__
UPDATE: As Sam points out I had the effeciency of the different techniques completely backwards. I also noticed a bug in the simple technique. Fixxing that speed it up by 50%.Rate split simple backref split 4329/s -- -31% -66% simple 6313/s 46% -- -50% backref 12658/s 192% 101% --
In reply to Re: regex search valid only if registers n and n+1 are equal?
by gam3
in thread regex search valid only if registers n and n+1 are equal?
by Voronich
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |