Why Compiled RegEx performance is slower than Intrepreted RegEx?

前端 未结 4 544
萌比男神i
萌比男神i 2020-12-05 07:47

I run into this article:

Performance: Compiled vs. Interpreted Regular Expressions, I modified the sample code to compile 1000 Regex and then run each 500 times to t

4条回答
  •  猫巷女王i
    2020-12-05 08:01

    Compiled regular expressions match faster when used as intended. As others have pointed out, the idea is to compile them once and use them many times. The construction and initialization time are amortized out over those many runs.

    I created a much simpler test that will show you that compiled regular expressions are unquestionably faster than not compiled.

        const int NumIterations = 1000;
        const string TestString = "some random text with email address, address@domain200.com";
        const string Pattern = "^[a-zA-Z0-9]+[a-zA-Z0-9._%-]*@domain0\\.\\.com$";
        private static Regex NormalRegex = new Regex(Pattern, RegexOptions.IgnoreCase);
        private static Regex CompiledRegex = new Regex(Pattern, RegexOptions.IgnoreCase | RegexOptions.Compiled);
        private static Regex DummyRegex = new Regex("^.$");
    
        static void Main(string[] args)
        {
            var DoTest = new Action((s, r, count) =>
                {
                    Console.Write("Testing {0} ... ", s);
                    Stopwatch sw = Stopwatch.StartNew();
                    for (int i = 0; i < count; ++i)
                    {
                        bool isMatch = r.IsMatch(TestString + i.ToString());
                    }
                    sw.Stop();
                    Console.WriteLine("{0:N0} ms", sw.ElapsedMilliseconds);
                });
    
            // Make sure that DoTest is JITed
            DoTest("Dummy", DummyRegex, 1);
            DoTest("Normal first time", NormalRegex, 1);
            DoTest("Normal Regex", NormalRegex, NumIterations);
            DoTest("Compiled first time", CompiledRegex, 1);
            DoTest("Compiled", CompiledRegex, NumIterations);
    
            Console.WriteLine();
            Console.Write("Done. Press Enter:");
            Console.ReadLine();
        }
    

    Setting NumIterations to 500 gives me this:

    Testing Dummy ... 0 ms
    Testing Normal first time ... 0 ms
    Testing Normal Regex ... 1 ms
    Testing Compiled first time ... 13 ms
    Testing Compiled ... 1 ms
    

    With 5 million iterations, I get:

    Testing Dummy ... 0 ms
    Testing Normal first time ... 0 ms
    Testing Normal Regex ... 17,232 ms
    Testing Compiled first time ... 17 ms
    Testing Compiled ... 15,299 ms
    

    Here you see that the compiled regular expression is at least 10% faster than the not compiled version.

    It's interesting to note that if you remove the RegexOptions.IgnoreCase from your regular expression, the results from 5 million iterations are even more striking:

    Testing Dummy ... 0 ms
    Testing Normal first time ... 0 ms
    Testing Normal Regex ... 12,869 ms
    Testing Compiled first time ... 14 ms
    Testing Compiled ... 8,332 ms
    

    Here, the compiled regular expression is 35% faster than the not compiled regular expression.

    In my opinion, the blog post you reference is simply a flawed test.

提交回复
热议问题