http://www.perlmonks.org?node_id=432311


in reply to Regex combining /(foo|bar)/ slower than using foreach (/foo/,/bar/) ???  

Hi, I think the problem is with this line.
my $regex = qr /(?:$regexstr)/;
because perl compiles this patern again & again in the foreach loop.
foreach my $stringlength (100,1000,10000,100000)
If you use this pattern
my $regex = qr /(?:$regexstr)/o;
instead of
my $regex = qr /(?:$regexstr)/;
It will be fast. Since it compiles the pattern only once.

Replies are listed 'Best First'.
Re^2: Regex combining /(foo|bar)/ slower than using foreach (/foo/,/bar/) ???
by Roy Johnson (Monsignor) on Feb 18, 2005 at 13:14 UTC
    No, Perl compiles the regex once where it is defined with qr.

    Caution: Contents may have been coded under pressure.