note 104391 added to regexp.reference.performance
| From: | inoutb at gmail dot com | Date: | Mon, 13 Jun 2011 11:34:31 +0000 |
| Subject: | note 104391 added to regexp.reference.performance | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-180472@lists.php.net to get a copy of this message | ||
There are at least three different algorithms that decide if and how a given regular expression
matches a string.
The oldest and fastest two rely on a result in formal language theory that allows every
nondeterministic finite automaton (NFA) to be transformed into a deterministic finite automaton
(DFA). The DFA can be constructed explicitly and then run on the resulting input string one symbol
at a time. Constructing the DFA for a regular expression of size m has the time and memory cost of
O(2m), but it can be run on a string of size n in time O(n). An alternative approach is to simulate
the NFA directly, essentially building each DFA state on demand and then discarding it at the next
step. This keeps the DFA implicit and avoids the exponential construction cost, but running cost
rises to O(m2n). The explicit approach is called the DFA algorithm and the implicit approach the NFA
algorithm. Adding caching to the NFA algorithm is often called the "lazy DFA" algorithm,
or just the DFA algorithm without making a distinction. These algorithms are fast, but using them
for recalling grouped subexpressions, lazy quantification, and similar features is tricky.[12][13]
The third algorithm is to match the pattern against the input string by backtracking. This algorithm
is commonly called NFA, but this terminology can be confusing. Its running time can be exponential,
which simple implementations exhibit when matching against expressions like (a|aa)*b that contain
both alternation and unbounded quantification and force the algorithm to consider an exponentially
increasing number of sub-cases. This behavior can cause a security problem called Regular expression
Denial of Service.
Although backtracking implementations only give an exponential guarantee in the worst case, they
provide much greater flexibility and expressive power. For example, any implementation which allows
the use of backreferences, or implements the various extensions introduced by Perl, must include
some kind of backtracking. Some implementations try to provide the best of both algorithms by first
running a fast DFA algorithm, and revert to a potentially slower backtracking algorithm only when a
backreference is encountered during the match.
source: http://en.wikipedia.org/wiki/Regular_expression#Implementations_and_running_times
----
Server IP: 69.147.83.197
Probable Submitter: 194.173.174.20
----
Manual Page -- http://www.php.net/manual/en/regexp.reference.performance.php
Edit -- https://master.php.net/note/edit/104391
Del: integrated -- https://master.php.net/note/delete/104391/integrated
Del: useless -- https://master.php.net/note/delete/104391/useless
Del: bad code -- https://master.php.net/note/delete/104391/bad+code
Del: spam -- https://master.php.net/note/delete/104391/spam
Del: non-english -- https://master.php.net/note/delete/104391/non-english
Del: in docs -- https://master.php.net/note/delete/104391/in+docs
Del: other reasons-- https://master.php.net/note/delete/104391
Reject -- https://master.php.net/note/reject/104391
Search -- https://master.php.net/manage/user-notes.php