C# Regex Performance very slow

前端 未结 3 520
醉梦人生
醉梦人生 2020-12-29 08:53

I am very new in regex topic. I want to parse log files with following regex:

(?
3条回答
  •  误落风尘
    2020-12-29 09:41

    If you are using the same regex multiple times, then make sure you compile it so that you are not recreating the regex each time. This can yield multiple orders of magnitude.

    var regex = new Regex(".*", RegexOptions.Compiled);
    

    The following LinqPad code shows 3 ways to use Regexes, from fastest to slowest.

    The regexFast method takes about 5 seconds, the regexSlow method takes 6 seconds and the regexSlowest takes about 50 seconds.

    void Main()
    {
        var sw = new Stopwatch();
    
        var regex = @"(?T[he]{2})\s*\w{5}.*";
    
        // This is the fastest method.
        sw.Restart();
        var regexFast = new Regex(regex, RegexOptions.Compiled);
        for (int i = 0; i < 9999999; i++)
        {
            regexFast.Match("The quick brown fox");
        }
        sw.Stop();
        sw.ElapsedMilliseconds.Dump();
    
        // This is a little slower - we didn't compile the regex so it has 
        // to do some extra work on each iteration.
        sw.Restart();
        var regexSlow = new Regex(regex);
        for (int i = 0; i < 9999999; i++)
        {
            regexSlow.Match("The quick brown fox");
        }
        sw.Stop();
        sw.ElapsedMilliseconds.Dump();
    
        // This method is super slow - we create a new Regex each time, so 
        // we have to do *lots* of extra work.
        sw.Restart();
        for (int i = 0; i < 9999999; i++)
        {
            var regexSlowest = new Regex(regex);
            regexSlowest.Match("The quick brown fox");
        }
        sw.Stop();
        sw.ElapsedMilliseconds.Dump();
    }
    

提交回复
热议问题