Why does Regex CompileToAssembly give slower performance than compiled regular expression and interpreted regular expression?

I use the following code to test the performance of CompileToAssembly against a compiled regular expression, but the results are not suitable. Please let me know what I am missing. Thanks!!!

static readonly Regex regex = new Regex(@"(stats|pause\s?(all|\d+(\,\d+)*)|start\s?(all|\d+(\,\d+)*)|add\s?time\s?(all|\d+(\,\d+)*)(\s\d+)|c(?:hange)?\s?p(?:asskey)?|close)(.*)", RegexOptions.Compiled); static readonly Regex reg = new Regex(@"(stats|pause\s?(all|\d+(\,\d+)*)|start\s?(all|\d+(\,\d+)*)|add\s?time\s?(all|\d+(\,\d+)*)(\s\d+)|c(?:hange)?\s?p(?:asskey)?|close)(.*)"); static readonly Regex level4 = new DuplicatedString(); static void Main() { const string str = "add time 243,3453,43543,543,534534,54534543,345345,4354354235,345435,34543534 6873brekgnfkjerkgiengklewrij"; const int itr = 1000000; CompileToAssembly(); Match match; Stopwatch sw = new Stopwatch(); sw.Start(); for (int i = 0; i < itr; i++) { match = regex.Match(str); } sw.Stop(); Console.WriteLine("RegexOptions.Compiled: {0}ms", sw.ElapsedMilliseconds); sw.Reset(); sw.Start(); for (int i = 0; i < itr; i++) { match = level4.Match(str); } sw.Stop(); Console.WriteLine("CompiledToAssembly: {0}ms", sw.ElapsedMilliseconds); sw.Reset(); sw.Start(); for (int i = 0; i < itr; i++) { match = reg.Match(str); } sw.Stop(); Console.WriteLine("Interpreted: {0}ms", sw.ElapsedMilliseconds); Console.ReadLine(); } public static void CompileToAssembly() { RegexCompilationInfo expr; List<RegexCompilationInfo> compilationList = new List<RegexCompilationInfo>(); // Define regular expression to detect duplicate words expr = new RegexCompilationInfo(@"(stats|pause\s?(all|\d+(\,\d+)*)|start\s?(all|\d+(\,\d+)*)|add\s?time\s?(all|\d+(\,\d+)*)(\s\d+)|c(?:hange)?\s?p(?:asskey)?|close)(.*)", RegexOptions.Compiled, "DuplicatedString", "Utilities.RegularExpressions", true); // Add info object to list of objects compilationList.Add(expr); // Apply AssemblyTitle attribute to the new assembly // // Define the parameter(s) of the AssemblyTitle attribute constructor Type[] parameters = { typeof(string) }; // Define the assembly title object[] paramValues = { "General-purpose library of compiled regular expressions" }; // Get the ConstructorInfo object representing the attribute constructor ConstructorInfo ctor = typeof(System.Reflection.AssemblyTitleAttribute).GetConstructor(parameters); // Create the CustomAttributeBuilder object array CustomAttributeBuilder[] attBuilder = { new CustomAttributeBuilder(ctor, paramValues) }; // Generate assembly with compiled regular expressions RegexCompilationInfo[] compilationArray = new RegexCompilationInfo[compilationList.Count]; AssemblyName assemName = new AssemblyName("RegexLib, Version=1.0.0.1001, Culture=neutral, PublicKeyToken=null"); compilationList.CopyTo(compilationArray); Regex.CompileToAssembly(compilationArray, assemName, attBuilder); } 

The following results follow:

 RegexOptions.Compiled: 3908ms CompiledToAssembly: 59349ms Interpreted: 5653ms 
+7
source share
2 answers

There is a problem in your code: static field initializers will execute before the static methods are run. This means that level4 already assigned before Main() starts. This means that the object referenced by level4 is not an instance of the class created in CompileToAssembly() .

Note that the sample code for Regex.CompileToAssembly shows the compilation of the regular expression and its consumption in two different programs. The actual regular expression that you chose as "CompiledToAssembly" may therefore be another regular expression that you compiled in an earlier test.

Another factor to consider: the overhead of loading the assembly into memory and crushing it into machine code can be significant enough for you to benefit from more than 1,000,000 iterations.

+6
source

You are working under a debugger (Visual Studio). This will prevent the implementation of JIT optimization during assembly. Try starting without a debugger (ctrl-f5).

+4
source

All Articles