in reply to Re: Precompiling qr/.../o question
in thread Precompiling qr/.../o question
Just to add a bit, a common performance pitfall is
@regexps = qw( foo bar baz ); @strings = qw( abc def ghi ); for $string (@strings) { for $regexp (@regexps) { $string =~ /$regexp/ } }
Each regexp is compiled for each element in @strings.
>perl -Mre=debug -e"@regexps = qw( foo bar baz ); @strings = qw( abc d +ef ghi ); for $string (@strings) { for $regexp (@regexps) { $string = +~ /$regexp/ } }" 2>&1 | find "Compiling" Compiling REx `foo' Compiling REx `bar' Compiling REx `baz' Compiling REx `foo' Compiling REx `bar' Compiling REx `baz' Compiling REx `foo' Compiling REx `bar' Compiling REx `baz'
Solution 1: Put the regexp loop on the outside:
@regexps = qw( foo bar baz ); @strings = qw( abc def ghi ); for $regexp (@regexps) { # All I did was reverse for $string (@strings) { # these two lines. $string =~ /$regexp/ } }
>perl -Mre=debug -e"@regexps = qw( foo bar baz ); @strings = qw( abc d +ef ghi ); for $regexp (@regexps) { for $string (@strings) { $string = +~ /$regexp/ } }" 2>&1 | find "Compiling" Compiling REx `foo' Compiling REx `bar' Compiling REx `baz'
Solution 2: Precompile the regexps:
@regexps = map qr/$_/, qw( foo bar baz ); # All I did was add the qr +//. @strings = qw( abc def ghi ); for $string (@strings) { for $regexp (@regexps) { $string =~ /$regexp/ } }
>perl -Mre=debug -e"@regexps = map qr/$_/, qw( foo bar baz ); @strings + = qw( abc def ghi ); for $string (@strings) { for $regexp (@regexps) + { $string =~ /$regexp/ } }" 2>&1 | find "Compiling" Compiling REx `foo' Compiling REx `bar' Compiling REx `baz'
|
|---|