Why is my regex so much slower compiled than interpreted?

前端 未结 5 694
花落未央
花落未央 2021-02-18 18:17

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

5条回答
  •  清酒与你
    2021-02-18 19:21

    To force initialization you can call Match against an empty string. On top of that you can use ngen to create a native image of the expression to speed up the process even further. But probably most importantly, it's essentially just as fast to throw 30.000 string.IndexOf's or string.Contains or Regex.Match statements against a given text, than compiling a ginormous big expression to Match against a single text. Since that requires a lot less compilation, jitting etc, as the state machine is a lot simpler.

    Another thing you could consider is to tokenize the text and intersect it with the list of words you're after.

提交回复
热议问题