Regular Expressions are powerful and typically fast. A recent script is using a set of about 1800 expressions (from a database) on roughly five million strings per day, typically 1 - 2 kB long. The RegEx matches take a lot of time and so I tried to speed them up. Working on the regular expression strings would be an option, but I also wanted to test if a methodic approach would help.

The expressions are stored and maintained in a database. They can't be compiled at script start unless I convert the database content to sourcecode (which would be possible in this case as they rarely change), but I was looking for an easier start.

RegExps could be precompiled even from strings:

my $content = 'foo(\w+)bar';my $re = qr/$content/i;$text =~ $re;

The second line compiles the expression and the last one just uses that precompiled stuff. Using strings as RegEx is a great injection chance, so be careful where your strings originate!

Regular expressions are powerful, but sometimes surprising. Using study sometimes even slows down things, so better test if precompiling is really faster before using it. Here is my benchmark script:

I tried different sample sets of expressions but always got at least 15% more performance for the precompiled ones. The difference gets slower when using /i to match case-insensitive, but only because insensitive matching reduces the overall speed and thus the influence of a longer compile time decreases.