Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy are C# compiled regular expressions faster than equivalent string methods?
    primarykey
    data
    text
    <p>Every time I have to do simple containment or replacement operations on strings, where the term that I'm searching for is a fixed value, I find that if I take my sample input and do some profiling on it, using a compiled regular expression is nearly* always faster than using the equivalent method from the String class. </p> <p>I've tried comparing a variety of methods ( <code>hs</code> is the "haystack" to search, <code>ndl</code> is the "needle" to search for, <code>repl</code> is the replacement value. <code>regex</code> is always created with the <code>RegexOptions.Compiled</code> option ):</p> <ul> <li><code>hs.Replace( ndl, repl )</code> vs <code>regex.Replace( hs, repl )</code></li> <li><code>hs.Contains( ndl )</code> vs <code>regex.IsMatch( hs )</code></li> </ul> <p>I've found quite a few discussions focusing on <em>which</em> of the two techniques are faster (<a href="https://stackoverflow.com/questions/3186285/c-sharp-which-is-faster-string-contains-or-regex-ismatch">1</a>, <a href="https://stackoverflow.com/questions/3601465/string-split-vs-regex-split">2</a>, <a href="https://stackoverflow.com/questions/9380062/is-using-a-regular-expression-faster-than-indexof">3</a>, and loads of others), but those discussions always seem to focus on:</p> <ol> <li>Use the string version for simple operations and regex for complex operations (which, from a raw performance perspective, doesn't even seem to be necessarily a good idea), or</li> <li>Run a test and compare the two ( and for equivalent tests, the regex version seems to always perform better ).</li> </ol> <p>I don't understand how this can possibly be the case: how does the regex engine compare any two strings for substring matches faster than the equivalent string version? This seems to hold true for search spaces that are very small or very large, or search terms that are small or large, or whether the search term occurs early or late in the search space.</p> <p>So, <strong>why</strong> are regular expressions faster?</p> <hr> <p>* In fact, the <strong>only</strong> case I've managed to show that the string version is faster than a compiled regex is when searching an empty string! Any other case, from single character strings to very long strings are processed faster by a compiled regex than the equivalent string method.</p> <hr> <p><strong>Update:</strong> Added a clause to clarify that I'm looking at cases where the search term is known at compile time. For dynamic or one-time operations, the overhead of compiling the regular expression will tend to skew the results in favor of the string methods.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload