in reply to regex search valid only if registers n and n+1 are equal?

And here is a benchmark for the 3 solutions.

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__
Rate split simple backref split 4329/s -- -31% -66% simple 6313/s 46% -- -50% backref 12658/s 192% 101% --
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%.
-- gam3
A picture is worth a thousand words, but takes 200K.

Replies are listed 'Best First'.
Re^2: regex search valid only if registers n and n+1 are equal?
by samtregar (Abbot) on Jul 01, 2006 at 16:10 UTC
    The backreference is much slower as you would expect.

    Really? Looks like the backreference is faster in your test.

    -sam