in reply to Re: Not sure what I am doing wrong
in thread Not sure what I am doing wrong

As his original post had stated that he wanted to look for certain strings, not patterns, I think that the
chomp(my @RE = <RECORDS>); my $RE = join '|', map quotemeta, @RE; $RE = qr/($RE)/;
will possibly be the most efficient matcher (at least on 5.10 ...), no?
[]s, HTH, Massa (κς,πμ,πλ)

Replies are listed 'Best First'.
Re^3: Not sure what I am doing wrong
by CountZero (Bishop) on Nov 25, 2008 at 22:46 UTC
    Even then, the less alternations you have the faster your regex will run. Of course if the strings or patterns have nothing in common it makes no difference.

    I did a quick test and got the following results:

    Rate Raw Regex Assembled Regex Raw Regex 614/s -- -9% Assembled Regex 676/s 10% --
    The text to check was part of a play by Shakespeare, loaded into an array (1 line per element) and the regex used was
    qr /this|that|here|there|where/;
    Assembled into a more efficient regex it looks like:
    qr /(?:th(?:ere|at|is)|w?here)/
    Even by eliminating only ONE of the alternations (5 vs 4) a 10% speed-up was obtained.

    PS: I do not know about 5.10, this was tested in 5.8.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James