in reply to Re^2: Problem with pre-compiled regex
in thread Problem with pre-compiled regex

Compiling a short and simple regex that consists of only one string literal is so fast that you'll probably have trouble to come up with a benchmark to measure it. So if there is a difference, it'll be too small to be easily observable.

I also think that Perl has an internal cache that avoids recompiling the same regex all over again.

Replies are listed 'Best First'.
Re^4: Problem with pre-compiled regex
by ikegami (Patriarch) on Jan 18, 2012 at 07:03 UTC

    I also think that Perl has an internal cache that avoids recompiling the same regex all over again.

    The ops that compile regex patterns (qr//, m// and s///) remember the last pattern they compiled.

    _ _ _ | | | | | >perl -Mre=debug -e"for (qw( a a a a b b b a )) { qr/$_/ }" 2>&1 | fin +d "Compiling" Compiling REx "a" Compiling REx "b" Compiling REx "a"

    So if you compile the same pattern many times in a row, you'll get savings.

    It's useful for code that looks like

    for my $pat (...) { for my $string (...) { ... $string =~ /$pat/ ... } }

    But not for code that looks like

    for my $string (...) { for my $pat (...) { ... $string =~ /$pat/ ... } }