Limiting Perl regular expressions to one compilation.
Here is one thing you can do to speed up the performance of your Perl regular expression matching: use the /o operator.
By default, when you iterate through a loop and apply a regular expression repeatedly, Perl recompiles the regular expression internally every time:
while() { chomp; if(/^[a-zA-Z]+/) { … } }
This is actually wise, as Perl makes no assumption about whether your pattern is dynamically generated. If it changes every time, the expression would require recompilation:
while() { chomp; my $pattern = get_upper_or_lower_pattern($_); if(/$pattern/) { … } }
But if you’re using the same pattern over and over, you may want to apply the /o operator in order to have the expression compiled only once:
while() { chomp; if(/^[a-zA-Z]+/o) { … } }
While my benchmarking suggests that the performance benefits are relatively negligible for a simple regex like the one above, they can be quite considerable for a pattern of greater complexity.