When you have code like this
# we read a line from somewhere to $line
# and @regex is our array of patterns
foreach (@regex) {
print "Match!\n" if $line=/$_/;
}
Each time the regex is run it has a different pattern so has to compile the pattern again. I belive compiling the pattern can be quite costly for complex regexen. However what I have done is first to expand all the patterns into a code block
# {
# my @matches;
# push @matches 1 if /first_regex/;
# push @matches 2 is /second_regex/;
# .
# .
# push @matches n if /nth_regex/;
# @matches;
# }
This code block is stored in a scalar and eval'd. It runs against the line stored in $_ and returns a list of indices telling us which regex was matched. Now Perl can see that it has a series of invariant regexen and after the first eval caches the compiled regexen and uses this version on subsequent itterations. the problem is still n*m omplexity but we have stopped perl doing rather a large amount of work in each itteration. Certainly for my problem here comparing over 400 lines of log to over 600 reasonably complex regexen the speed up was >10 times.
Cheers,
R.