When considering on-the-fly compilation with RegexOptions.Compiled, there are important tradeoffs among initial startup time, ongoing memory usage, and regex match speed:
| Metric | Without RegexOptions.Compiled | With RegexOptions.Compiled |
|---|---|---|
| Startup time | Faster | Slower (by 60×) |
| Memory usage | Low | High (about 5-15k each) |
| Match speed | Not as fast | Up to 10× faster |
The initial regex parsing (the default kind, without RegexOptions.Compiled) that must be done the first time each regex is seen in the program is relatively fast.
Even on my clunky old 550MHz NT box, I benchmark about 1,500 complex compilations/ second. When RegexOptions.Compiled is used, that goes down to
about 25/second, and increases memory usage by about 10k bytes per regex. More importantly, that memory remains used for the life of the program — there is no way to unload the compiled Regex code.
RegexOptions.compiled is best used:
- In tight loops called many times
- On large blocks of text
Sources: