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>();
The following results follow:
RegexOptions.Compiled: 3908ms CompiledToAssembly: 59349ms Interpreted: 5653ms
iRock
source share