Skip to content

Instantly share code, notes, and snippets.

@jadeallenx
Last active July 20, 2016 22:15
Show Gist options
  • Save jadeallenx/8bc1381ab30ae28701644b0a487ad1b2 to your computer and use it in GitHub Desktop.
Save jadeallenx/8bc1381ab30ae28701644b0a487ad1b2 to your computer and use it in GitHub Desktop.
Perl regular expression performance

In response to an outage on Stack Overflow Rob Pike tweeted that regex parsing failure was avoidable and pointed toward a great post on regex complexity by Russ Cox. The version of perl in that post (5.8.7) is super ancient and in perl 5.10 the regex engine received a ton of optimization work and that optimization work has continued right through to the most recent release - 5.24.0.

Running the script above on 5.24.0 shows:

shadowfax:~ mallen$ time perl re.pl 
match

real	0m0.099s
user	0m0.026s
sys	0m0.039s

If you're looking to explore, debug or optimize your perl regular expressions, let me strongly recommend Damian Conway's RegExp::Debugger You can still make pathological regular expressions of course but it's much more difficult than the patterns given in Russ Cox's post.

# tested on 5.24.0 and 5.10.1 - should be true for all versions >5.10
use 5.024;
my $str = "a" x 100_000;
my $re = qr/a?a?a?aaa/;
if ( $str =~ $re ) {
say "match";
}
else {
say "no match"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment