Why Compiled RegEx performance is slower than Intrepreted RegEx?

前端 未结 4 535
萌比男神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条回答
  •  粉色の甜心
    2020-12-05 08:14

    This is almost certainly an indication that your benchmark code is written incorrectly vs. compiled regex's being slower than interpreted ones. There is a lot of work that went into making compiled regex's performant.

    Now that we have the code can look at a few specific things that need updating

    1. This code doesn't account for the JIT costs of the method. It should run the code once to get the JIT costs out of the way and then run it again and measure
    2. Why is lock used at all? It's completely unnecessary
    3. Benchmarks should use StopWatch not DateTime
    4. To get a good comparison between Compiled and not compiled you should test the performance of a single compiled Regex and single non-compiled Regex matching N times. Not N of each matching at most once per regex.

提交回复
热议问题