3

I was reading this article on dotnetperls.com about regular expressions, when I read the following: (emphasis mine)

...we see that using a Regex instance object is faster than using the static Regex.Match. For performance, you should always use an instance object.

I would have expected that using the static method would be faster, because I'd suspect that they do the regex.match identical, but an instance needs initialization (which takes time of course).

After some searching I atleast found out that (part of) my gut feeling was right. Concerning static methods, this dotnerperls article states: (emphasis mine)

Static methods have no instances. They are called with the type name, not an instance identifier. They are slightly faster than instance methods because of this.

But then why would a RegEx instance be faster than the static class?

2
  • 3
    Have a look at this answer stackoverflow.com/questions/414328/… Commented Nov 29, 2013 at 7:40
  • 1
    @Pengu, thanks that answer answers my question aswell. Regarding the person who gave me a close vote. I would like to point out that the questions aren't the same. The linked question asks whether you should use static or instance and mine is why static is faster. The fact that the accepted answer also answers my question is fortunate, but that doesn't make this question a duplicate. Commented Nov 29, 2013 at 7:51

1 Answer 1

2

For a static Regex match to be performed the Regex object needs to convert your pattern into a structure that can be executed (by creating an internal Regex instance) and the string that you pass into Match needs to be searched, in addition to this the internal Regex instance is cached to allow faster execution on subsequent calls. So the first time you execute the static Match the time taken is approximately:

  • Check Regex cache for reusable instance (will not find one)
  • Create internal Regex instance
  • Cache Regex instance
  • Execute Match

Subsequent calls are more or less:

  • Check Regex cache for reusable instance (will find one)
  • Execute Match using cached instance

With an instance object you create an instance and call match on that instance - no time is spent looking at or managing the internal cache, each subsequent call on the instance only executes the match logic. So in this case an instance match will always be faster than a static match.

In general calling a static method will be (marginally) faster than calling an equivalent instance method but only if the methods do exactly the same thing, which is unlikely since a static method cannot have instance state...

Sign up to request clarification or add additional context in comments.

1 Comment

Nice quick-reference answer, especially for pointing out that the info about static performance is mostly theoretically. I'd also like to advice people who also had this question to read the accepted answer in the link for a more detailed explanation of RegEx caching.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.