ben.biddington

With respect to the Regex cache, is there any advantage of using a static Regex instance for frequent matching

For example I have a case where I know the pattern will never change at runtime, so I figure the most efficient approach is to share a static Regex object.

This is surely more efficient than creating new instances and relying on the Regex class cache

<bb />


Re: Regular Expressions Regular expression best practice

OmegaMan

Which performance are you interested in Load time or match time. If it is match time...this discussion is moot. If it is load...according to the post you referenced

Regular expressions created by static methods are cached in 2.0 as users of the static methods do not have any way of managing the lifetime of their regular expressions. Developers that want the full control of managing the lifetime of their regular expressions should use Regex instances instead of Regex static methods.


Which means it will be cached regardless of whether it exists in one place or different places, the caching mechanism will find the similar ones.

The only other performance enhancement would be to try have it compiled into the assembly (pre-compiled), so that on-the-fly compilation does not have to be done for the first shot at least.

But alas, caching an on-the-fly/precompilation of the regex does not change the performance of the match! These only affect the creation of the item.

Hope this helps...