The RegexOptions.Compiled Flag and Slow Performance on 64-Bit .NET Framework 2.0 [Josh Free]
The performance problem occurs in the Regex(String pattern, RegexOptions options) constructor when instantiating very large, un-optimized regular expressions and while specifying the RegexOptions.Compiled flag:
private static Regex nonwords = new Regex(@"\b("
+@")\b", (RegexOptions.IgnoreCase | RegexOptions.Compiled));
The compilation performance problem in the 64-Bit .NET Framework 2.0 is fixed with this hotfix http://support.microsoft.com/kb/917507, and will be released broadly in Service Pack 1 of .NET Framework 2.0.
There are also several workarounds to this issue.
Reduce the Regular Expression Pattern
Developers can reduce the size of their regular expressions by simplifying the expression. For instance the un-optimized pattern
can be replaced with this pattern:
Use Regex Pre-Compilation Instead of Compiling-on-the-Fly
Developers can use Regex.CompileToAssembly to build an assembly containing their regular expression, instead of always compiling the regular expression during application startup. For more details on Regular Expression Compilation options please see the CLR Inside Out article in the January 2006 edition of MSDN Magazine.
Remove the RegexOptions.Compiled Flag From Your Code
If you have never profiled the performance of your application or if you have profiled your app, and the run-time bottleneck is not Regex, you can consider dropping the RegexOptions.Compiled flag as a workaround, until .NET Framework 2.0 Service Pack 1 is released.