Op 30 Mar 2006 00:38:08 -0800 schreef Jack:
> I found out that using the static Regex.Matches within the loop doesn't
> have this performance issue.
[quoted text clipped - 15 lines]
> variable against just using the static method.
> Does anybody know why?
Jack,
The Regex class keeps a cache of compiled regular expressions. Using the
static matches method, the regular expression is only compiled once at the
first call. The next calls will see that the regular expression is already
in the cache. If you set the regex.cachesize to 0 (this disables the cache)
your program will take a lot longer.
I cannot find it in the documentation, but if you use a RegEx instance, the
compiled expression is not cached in the class. It is just cached in the
instance. If the instance disappears, the compiled regular expression
disappears. Because of that, your code that repeatedly instantiates a RegEx
variable will cause the expression to be compiled every time.
Another solution would be to use one RegEx instance that is created outside
the loop.
Renze de Waal.
Jack - 31 Mar 2006 00:06 GMT
Renze,
Thanks for ur explanation. That does explain why I get the performance
issue.
But imo the caching if static is a strange bonus compared to using an
instance. But this recompiling doesn't happen when I get the Count
property right? It should be at instantiating the instance or at least
at calling the Matches method.
Anyway, I think I'll just use the static Regex.Matches method.
And yes, if I take the instance out of the loop I can use the
instance...need to rewrite some (one else's) code since the Regex is
part of a custom object.
Thanks!