Regular Expression Performance Comparison

The following tables provide comparisons between the following regular expression libraries:

GRETA.

The Boost regex library.

Henry Spencer's regular expression library - this is provided for comparison as a typical non-backtracking implementation.

Philip Hazel's PCRE library.

Details

Machine: Intel Xeon X5560 2.8GHz Server.

Compiler: GNU C++ version 4.1.2 20080704 (Red Hat 4.1.2-50).

C++ Standard Library: GNU libstdc++ version 20080704.

OS: linux.

Boost version: 1.46.1.

PCRE version: 8.10.

As ever care should be taken in interpreting the results, only sensible regular expressions (rather than pathological cases) are given, most are taken from the Boost regex examples, or from the Library of Regular Expressions. In addition, some variation in the relative performance of these libraries can be expected on other machines - as memory access and processor caching effects can be quite large for most finite state machine algorithms.

Averages

The following are the average relative scores for all the tests: the perfect regular expression library would score 1, in practice anything less than 2 is pretty good.

GRETAGRETA
(non-recursive mode)
BoostBoost + C++ localePOSIXPCREDynamic Xpressive
1.95012 3.27549 2.08255 2.1151 nan 1.62895 1.14076

Comparison 1: Long Search

For each of the following regular expressions the time taken to find all occurrences of the expression within a long English language text was measured (mtent12.txt from Project Gutenberg, 19Mb). 

ExpressionGRETAGRETA
(non-recursive mode)
BoostBoost + C++ localePOSIXPCREDynamic Xpressive
Twain1.54
(0.0231s)
1.54
(0.0231s)
1.54
(0.0231s)
1.52
(0.0228s)
NA1
(0.015s)
1.17
(0.0175s)
Huck[[:alpha:]]+1.44
(0.0203s)
1.44
(0.0203s)
1.51
(0.0213s)
1.49
(0.0209s)
NA1
(0.0141s)
1.2
(0.0169s)
[[:alpha:]]+ing2.95
(0.84s)
6.07
(1.73s)
1.47
(0.42s)
1.47
(0.42s)
NA4.21
(1.2s)
1
(0.285s)
^[^ ]*?Twain2.32
(0.325s)
5.86
(0.82s)
1.11
(0.155s)
1
(0.14s)
NA1.86
(0.26s)
1.04
(0.145s)
Tom|Sawyer|Huckleberry|Finn4.57
(0.14s)
8
(0.245s)
1
(0.0306s)
1
(0.0306s)
NA1.18
(0.0362s)
1.04
(0.0319s)
(Tom|Sawyer|Huckleberry|Finn).{0,30}river|river.{0,30}(Tom|Sawyer|Huckleberry|Finn)2.33
(0.16s)
4.29
(0.295s)
1.4
(0.0963s)
1.33
(0.0912s)
NA1.96
(0.135s)
1
(0.0688s)

Comparison 2: Medium Sized Search

For each of the following regular expressions the time taken to find all occurrences of the expression within a medium sized English language text was measured (the first 50K from mtent12.txt - up to the end of Chapter 1). 

ExpressionGRETAGRETA
(non-recursive mode)
BoostBoost + C++ localePCREDynamic Xpressive
Twain1.26
(5.55e-05s)
1.26
(5.55e-05s)
2.64
(0.000116s)
2.64
(0.000116s)
1.58
(6.96e-05s)
1
(4.39e-05s)
Huck[[:alpha:]]+1.38
(6.04e-05s)
1.38
(6.04e-05s)
1.83
(8.06e-05s)
1.69
(7.45e-05s)
1.14
(5e-05s)
1
(4.39e-05s)
[[:alpha:]]+ing3.22
(0.00227s)
6.56
(0.00461s)
1.35
(0.000947s)
1.33
(0.000937s)
4.56
(0.0032s)
1
(0.000703s)
^[^ ]*?Twain2.2
(0.000869s)
5.43
(0.00215s)
1.02
(0.000405s)
1
(0.000396s)
1.78
(0.000703s)
1
(0.000396s)
Tom|Sawyer|Huckleberry|Finn2.28
(0.000435s)
4.1
(0.000781s)
1.31
(0.000249s)
1.33
(0.000254s)
1.41
(0.000269s)
1
(0.00019s)
(Tom|Sawyer|Huckleberry|Finn).{0,30}river|river.{0,30}(Tom|Sawyer|Huckleberry|Finn)1.59
(0.000498s)
3.03
(0.000947s)
1.62
(0.000508s)
1.69
(0.000527s)
1.75
(0.000547s)
1
(0.000313s)

Comparison 3: C++ Code Search

For each of the following regular expressions the time taken to find all occurrences of the expression within the C++ source file boost/crc.hpp was measured. 

ExpressionGRETAGRETA
(non-recursive mode)
BoostBoost + C++ localePCREDynamic Xpressive
^(template[[:space:]]*<[^;:{]+>[[:space:]]*)?(class|struct)[[:space:]]*(\<\w+\>([ ]*\([^)]*\))?[[:space:]]*)*(\<\w*\>)[[:space:]]*(<[^;:{]+>[[:space:]]*)?(\{|:[^;\{()]*\{)11.7
(0.000918s)
11.7
(0.000918s)
1.03
(8.06e-05s)
1.05
(8.18e-05s)
2.31
(0.000181s)
1
(7.81e-05s)
(^[ ]*#(?:[^\\\n]|\\[^\n_[:punct:][:alnum:]]*[\n[:punct:][:word:]])*)|(//[^\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)\>1
(0.0025s)
1
(0.0025s)
1.27
(0.00316s)
1.3
(0.00324s)
2
(0.005s)
NA
^[ ]*#[ ]*include[ ]+("[^"]+"|<[^>]+>)2.95
(0.000288s)
9.7
(0.000947s)
1.38
(0.000134s)
1.4
(0.000137s)
1.8
(0.000176s)
1
(9.77e-05s)
^[ ]*#[ ]*include[ ]+("boost/[^"]+"|<boost/[^>]+>)2.95
(0.000288s)
9.7
(0.000947s)
1.38
(0.000134s)
1.38
(0.000134s)
1.8
(0.000176s)
1
(9.77e-05s)

Comparison 4: HTML Document Search

For each of the following regular expressions the time taken to find all occurrences of the expression within the html file libs/libraries.htm was measured. 

ExpressionGRETAGRETA
(non-recursive mode)
BoostBoost + C++ localePCREDynamic Xpressive
beman|john|dave1.95
(0.000371s)
3.44
(0.000654s)
1.18
(0.000225s)
1.17
(0.000222s)
1
(0.00019s)
1.26
(0.000239s)
<p>.*?</p>1
(5.49e-05s)
1.02
(5.62e-05s)
2.58
(0.000142s)
2.53
(0.000139s)
1.56
(8.54e-05s)
1.82
(0.0001s)
<a[^>]+href=("[^"]*"|[^[:space:]]+)[^>]*>1.04
(0.000254s)
1.48
(0.000361s)
1.34
(0.000327s)
1.4
(0.000342s)
1
(0.000244s)
1.4
(0.000342s)
<h[12345678][^>]*>.*?</h[12345678]>1.11
(0.000101s)
1.2
(0.00011s)
1.55
(0.000142s)
1.57
(0.000144s)
1
(9.16e-05s)
1.71
(0.000156s)
<img[^>]+src=("[^"]*"|[^[:space:]]+)[^>]*>1
(5.13e-05s)
1.02
(5.25e-05s)
2.52
(0.000129s)
2.67
(0.000137s)
1.6
(8.18e-05s)
1.5
(7.69e-05s)
<font[^>]+face=("[^"]*"|[^[:space:]]+)[^>]*>.*?</font>1
(4.15e-05s)
1.03
(4.27e-05s)
3
(0.000125s)
3.12
(0.000129s)
1.97
(8.18e-05s)
1.68
(6.96e-05s)

Comparison 3: Simple Matches

For each of the following regular expressions the time taken to match against the text indicated was measured. 

ExpressionTextGRETAGRETA
(non-recursive mode)
BoostBoost + C++ localePCREDynamic Xpressive
abcabc1
(5.66e-08s)
1.39
(7.87e-08s)
2.78
(1.57e-07s)
2.82
(1.6e-07s)
1.28
(7.27e-08s)
1.24
(7.03e-08s)
^([0-9]+)(\-| |$)(.*)$100- this is a line of ftp response which contains a message string1.36
(1.72e-07s)
2.87
(3.62e-07s)
3.28
(4.15e-07s)
3.28
(4.15e-07s)
1.68
(2.12e-07s)
1
(1.26e-07s)
([[:digit:]]{4}[- ]){3}[[:digit:]]{3,4}1234-5678-1234-4561.41
(2.62e-07s)
2
(3.72e-07s)
2.97
(5.53e-07s)
2.82
(5.25e-07s)
1.13
(2.1e-07s)
1
(1.86e-07s)
^([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})(\]?)$john@johnmaddock.co.uk1.74
(7.63e-07s)
1.74
(7.63e-07s)
2.26
(9.92e-07s)
2.26
(9.92e-07s)
1.17
(5.15e-07s)
1
(4.39e-07s)
^([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})(\]?)$foo12@foo.edu1.95
(6.77e-07s)
1.89
(6.58e-07s)
2.44
(8.49e-07s)
2.49
(8.68e-07s)
1.33
(4.63e-07s)
1
(3.48e-07s)
^([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})(\]?)$bob.smith@foo.tv1.92
(6.68e-07s)
1.92
(6.68e-07s)
2.41
(8.39e-07s)
2.47
(8.58e-07s)
1.29
(4.48e-07s)
1
(3.48e-07s)
^[a-zA-Z]{1,2}[0-9][0-9A-Za-z]{0,1} {0,1}[0-9][A-Za-z]{2}$EH10 2QQ1
(9.89e-08s)
1.86
(1.84e-07s)
3.04
(3e-07s)
3.18
(3.15e-07s)
1.33
(1.31e-07s)
1.04
(1.03e-07s)
^[a-zA-Z]{1,2}[0-9][0-9A-Za-z]{0,1} {0,1}[0-9][A-Za-z]{2}$G1 1AA1
(9.89e-08s)
1.83
(1.81e-07s)
2.75
(2.72e-07s)
2.8
(2.77e-07s)
1.33
(1.31e-07s)
1.05
(1.04e-07s)
^[a-zA-Z]{1,2}[0-9][0-9A-Za-z]{0,1} {0,1}[0-9][A-Za-z]{2}$SW1 1ZZ1
(9.89e-08s)
1.86
(1.84e-07s)
2.84
(2.81e-07s)
2.89
(2.86e-07s)
1.33
(1.31e-07s)
1.04
(1.03e-07s)
^[[:digit:]]{1,2}/[[:digit:]]{1,2}/[[:digit:]]{4}$4/1/20011
(8.94e-08s)
2.08
(1.86e-07s)
2.88
(2.57e-07s)
2.93
(2.62e-07s)
1.36
(1.22e-07s)
1.16
(1.04e-07s)
^[[:digit:]]{1,2}/[[:digit:]]{1,2}/[[:digit:]]{4}$12/12/20011
(8.94e-08s)
2.11
(1.88e-07s)
2.99
(2.67e-07s)
3.15
(2.81e-07s)
1.39
(1.24e-07s)
1.17
(1.05e-07s)
^[-+]?[[:digit:]]*\.?[[:digit:]]*$1231
(7.99e-08s)
2
(1.6e-07s)
2.87
(2.29e-07s)
3.22
(2.57e-07s)
1.61
(1.29e-07s)
1.18
(9.42e-08s)
^[-+]?[[:digit:]]*\.?[[:digit:]]*$+3.141591
(9.18e-08s)
2.05
(1.88e-07s)
3.22
(2.96e-07s)
3.38
(3.1e-07s)
1.48
(1.36e-07s)
1.12
(1.03e-07s)
^[-+]?[[:digit:]]*\.?[[:digit:]]*$-3.141591
(9.18e-08s)
2.03
(1.86e-07s)
3.22
(2.96e-07s)
3.38
(3.1e-07s)
1.48
(1.36e-07s)
1.13
(1.04e-07s)


?? Copyright John Maddock 2003

Use, modification and distribution are subject to the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)