Regular Expression Performance Comparison



The following document provides performance comparison for the following regular expression libraries:


What is tested

Most of the tests performed are taken from a de facto standard regexp engine test set available from the Web: computing times are measured when matching small texts or searching medium/big size texts for expression occurrences. The last test measures the performance decrease with the increase of the expression complexity, namely the number of alternatives in the expression.

Architecture

Linux localhost.localdomain 2.6.30.9-99.fc11.x86_64 #1 SMP Tue Nov 17 21:30:38 EST 2009 x86_64 x86_64 x86_64 GNU/Linux

processor : 0
vendor_id : GenuineIntel
cpu family : 6
model : 30
model name : Intel(R) Core(TM) i5 CPU 750 @ 2.67GHz
stepping : 5
cpu MHz : 1197.000
cache size : 8192 KB
physical id : 0
siblings : 4
core id : 0
cpu cores : 4
apicid : 0
initial apicid : 0
fpu : yes
fpu_exception : yes
cpuid level : 11
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx rdtscp lm constant_tsc arch_perfmon pebs bts rep_good xtopology tsc_reliable nonstop_tsc pni dtes64 monitor ds_cpl vmx smx est tm2 ssse3 cx16 xtpr pdcm sse4_1 sse4_2 lahf_lm ida tpr_shadow vnmi flexpriority ept vpid
bogomips : 5467.63
clflush size : 64
cache_alignment : 64
address sizes : 36 bits physical, 48 bits virtual
power management:



SHORT MATCHES

The following tests evaluate the time taken to match the expression to the input string.
For each result, the top number has been normalized relative to the fastest time, so 1 is as good as it gets.

astlboost::regexboost::xpressivegretapcreTextExpression
2.611112.666671.4166711.13889100- this is a line of ftp response which contains a message string^([0-9]+)(\-| |$)(.*)$
13.192312.1538521.576921234-5678-1234-456([[:digit:]]{4}[- ]){3}[[:digit:]]{3,4}
13.5753.1252.4252.675john_maddock@compuserve.com^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$
16.65.84.24.9foo12@foo.edu^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$
15.484.643.363.68bob.smith@foo.tv^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$
14.733331.866671.533332.2EH10 2QQ^[a-zA-Z]{1,2}[0-9][0-9A-Za-z]{0,1} {0,1}[0-9][A-Za-z]{2}$
15.083332.333331.666672.75G1 1AA^[a-zA-Z]{1,2}[0-9][0-9A-Za-z]{0,1} {0,1}[0-9][A-Za-z]{2}$
14.615382.153851.846152.53846SW1 1ZZ^[a-zA-Z]{1,2}[0-9][0-9A-Za-z]{0,1} {0,1}[0-9][A-Za-z]{2}$
13.666671.81.533331.84/1/2001^[[:digit:]]{1,2}/[[:digit:]]{1,2}/[[:digit:]]{4}$
13.294121.647061.352941.6470612/12/2001^[[:digit:]]{1,2}/[[:digit:]]{1,2}/[[:digit:]]{4}$
17.53.252.1253.5123^[-+]?[[:digit:]]*\.?[[:digit:]]*$
15.066671.81.333332+3.14159^[-+]?[[:digit:]]*\.?[[:digit:]]*$
15.066671.81.333331.93333-3.14159^[-+]?[[:digit:]]*\.?[[:digit:]]*$

SUMMARY FOR SHORT MATCHES

These are average values: the perfect library would get 1, which means best performance for all tests.

astl1.12393
greta1.97762
pcre2.48767
boost::xpressive2.59896
boost::regex4.65693



LONG MATCHES

The next test measures the time to find all matches in a long English text. The text is the complete works of Mark Twain, from Project Gutenberg. The text is 19Mb long.
For each result, the top number has been normalized relative to the fastest time, so 1 is as good as it gets.

astlboost::regexboost::xpressivegretapcreTextExpression
11.21429111.21429complete works of Mark TwainTwain
11.307691.076921.076921.23077complete works of Mark TwainHuck[[:alpha:]]+
12.405662.05667.4622611.1887complete works of Mark Twain[[:alpha:]]+ing
2.763161.3684214.447371.97368complete works of Mark Twain^[^ ]*?Twain
2.2173911.130434.739131.3913complete works of Mark TwainTom|Sawyer|Huckleberry|Finn
1.21.1090912.42.36364complete works of Mark Twain(Tom|Sawyer|Huckleberry|Finn).{0,30}river|river.{0,30}(Tom|Sawyer|Huckleberry|Finn)

SUMMARY FOR LONG MATCHES

These are average values: the perfect library would get 1, which means best performance for all tests.

boost::xpressive1.21066
boost::regex1.40086
astl1.53009
pcre3.22706
greta3.52095



CODE SEARCH

For each of the following regular expressions the time taken to find all occurrences of the expression within the boost C++ source file crc.hpp was measured.
For each result, the top number has been normalized relative to the fastest time, so 1 is as good as it gets.

astlboost::regexboost::xpressivegretapcreTextExpression
3.3846211.230775.076922.46154/home/vintz/src/sourceforge/svn/astl/trunk/bench/crc.hpp^(template[[:space:]]*<[^;:{]+>[[:space:]]*)?(class|struct)[[:space:]]*(\<\w+\>([ ]*\([^)]*\))?[[:space:]]*)*(\<\w*\>)[[:space:]]*(<[^;:{]+>[[:space:]]*)?(\{|:[^;\{()]*\{)
111.953510.79075.7674424.3953/home/vintz/src/sourceforge/svn/astl/trunk/bench/crc.hpp(^[ ]+#(?:[^\\\n]|\\[^\n_[:punct:][:alnum:]]*[\n[:punct:][:alnum:]_])*)|(//[^\n]*|/\*.*?\*/)|\<([+-]?(?:(?:0x[[:xdigit:]]+)|(?:(?:[[:digit:]]*\.)?[[:digit:]]+(?:[eE][+-]?[[:digit:]]+)?))u?(?:(?:int(?:8|16|32|64))|L)?)\>|('(?:[^\\']|\\.)*'|"(?:[^\\"]|\\.)*")|\<(__asm|__cdecl|__declspec|__export|__far16|__fastcall|__fortran|__import|__pascal|__rtti|__stdcall|_asm|_cdecl|__except|_export|_far16|_fastcall|__finally|_fortran|_import|_pascal|_stdcall|__thread|__try|asm|auto|bool|break|case|catch|cdecl|char|class|const|const_cast|continue|default|delete|do|double|dynamic_cast|else|enum|explicit|extern|false|float|for|friend|goto|if|inline|int|long|mutable|namespace|new|operator|pascal|private|protected|public|register|reinterpret_cast|return|short|signed|sizeof|static|static_cast|struct|switch|template|this|throw|true|try|typedef|typeid|typename|union|unsigned|using|virtual|void|volatile|wchar_t|while)\>
2.263161.0526313.684211.42105/home/vintz/src/sourceforge/svn/astl/trunk/bench/crc.hpp^[ ]*#[ ]*include[ ]+("[^"]+"|<[^>]+>)
2.263161.0526313.684211.42105/home/vintz/src/sourceforge/svn/astl/trunk/bench/crc.hpp^[ ]*#[ ]*include[ ]+("boost/[^"]+"|]+>)

SUMMARY FOR CODE SEARCH

These are average values: the perfect library would get 1, which means best performance for all tests.

astl2.22773
boost::xpressive3.50537
boost::regex3.76469
greta4.5532
pcre7.42475



HTML SEARCH

For each of the following regular expressions the time taken to find all occurrences of the expression within the boost html documentation file libs/libraries.htm was measured.
For each result, the top number has been normalized relative to the fastest time, so 1 is as good as it gets.

astlboost::regexboost::xpressivegretapcreTextExpression
1.043481.1739112.60871.6087/home/vintz/src/sourceforge/svn/astl/trunk/bench/libraries.htmbeman|john|dave
11.91.212.1/home/vintz/src/sourceforge/svn/astl/trunk/bench/libraries.htm<p>.*?</p>
11.51.51.821431.92857/home/vintz/src/sourceforge/svn/astl/trunk/bench/libraries.htm<a[^>]+href=("[^"]*"|[^[:space:]]+)[^>]*>
11.117651.0588211.35294/home/vintz/src/sourceforge/svn/astl/trunk/bench/libraries.htm<h[12345678][^>]*>.*?</h[12345678]>
12.857141.142861.285712.85714/home/vintz/src/sourceforge/svn/astl/trunk/bench/libraries.htm<img[^>]+src=("[^"]*"|[^[:space:]]+)[^>]*>
12.714291.1428612.85714/home/vintz/src/sourceforge/svn/astl/trunk/bench/libraries.htm<font[^>]+face=("[^"]*"|[^[:space:]]+)[^>]*>.*?</font>

SUMMARY FOR HTML SEARCH

These are average values: the perfect library would get 1, which means best performance for all tests.

astl1.00725
boost::xpressive1.17409
greta1.45264
boost::regex1.87716
pcre2.11742



OVERALL SUMMARY

These are average values: the perfect library would get 1, which means best performance for all tests.

astl1.33607
boost::xpressive2.14194
greta2.54357
pcre3.24502
boost::regex3.28507



SCALING

The next test evaluates the behavior of the libraries when the complexity of the expression increases: starting from a simple expression, at each step a new expression is concatenated as an alternative and the time to find all matches 10 times is measured.

Step 1: [[:upper:]]\. +[[:upper:]]\. +
Step 2: [[:upper:]]\. +[[:upper:]]\. +|Mr\.? [[:upper:]][[:lower:]]+ ([[:upper:]][[:lower:]] )*
Step 3: [[:upper:]]\. +[[:upper:]]\. +|Mr\.? [[:upper:]][[:lower:]]+ ([[:upper:]][[:lower:]] )*|days?
...
Step 20: [[:upper:]]\. +[[:upper:]]\. +|Mr\.? [[:upper:]][[:lower:]]+ ([[:upper:]][[:lower:]] )*|days?|P\. *S\.--|[[:upper:]][[:alpha:]]+ +[Ii]sles?|[$£]\d+([.,]\d+)*|"[[:alnum:]]+"|Mrs\.? [[:upper:]][[:lower:]]+ ([[:upper:]][[:lower:]] )*|(\d|1[0-2]) ?[aApP]\.?[Mm]\.?|([aA]\.?[Dd]|[bB]\.?[Cc])\.? \d{3,4}|[[:upper:]][[:lower:]]+[!?]|[[:upper:]]+( [[:upper:]]+)*|[hH]orses?|[oO]nce upon a time|Twain|(?i:CHAPTER [IVXLDC]+)|good terms?|Section \d+|New York|[Dd][Ee][Aa][Rr]

The text is the complete works of Mark Twain, from Project Gutenberg. The text is 19Mb long.