Why is my regex so much slower compiled than interpreted ?

Posted by miket2e on Stack Overflow See other posts from Stack Overflow or by miket2e
Published on 2010-12-27T11:48:01Z Indexed on 2010/12/27 11:54 UTC
Read the original article Hit count: 151

Filed under:
|
|

I have a large and complex C# regex that runs OK when interpreted, but is a bit slow. I'm trying to speed this up by setting RegexOptions.Compiled, and this seems to take about 30 seconds for the first time and instantly after that. I'm trying to negate this by compiling the regex to an assembly first, so my app can be as fast as possible.

My problem is when the compiling delay takes place:

Regex myComplexRegex = new Regex(regexText, RegexOptions.Compiled);
MatchCollection matches = myComplexRegex.Matches(searchText);
foreach (Match match in matches) // <--- when the one-time long delay kicks in
{

} 

This is making compiling to an assembly basically useless, as I still get the delay on the first foreach call. What I want is for all the compiling delay to be done in advance when I compile to the assembly, not when the user runs the app. Where am I going wrong ?

(The code I'm using to compile to an assembly is similar to http://www.dijksterhuis.org/regular-expressions-advanced/ , if that's relevant ).

© Stack Overflow or respective owner

Related posts about c#

Related posts about regex