uncached:
var sw = Stopwatch.StartNew(); foreach (var str in testStrings) { foreach (var pair in flex) { if (Regex.IsMatch(str, "^(" + pair.Value + ")$", RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture)) ; } } Console.WriteLine("\nRan in {0} ms", sw.ElapsedMilliseconds);
Saved Copy
var cache = flex.ToDictionary(p => p.Key, p => new Regex("^(" + p.Value + ")$", RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture | RegexOptions.Compiled)); var sw = Stopwatch.StartNew(); foreach (var str in testStrings) { foreach (var pair in cache) { if(pair.Value.IsMatch(str)) ; } } Console.WriteLine("\nRan in {0} ms", sw.ElapsedMilliseconds);
I don't know why it works slower when I precompile all regular expressions. Not to mention that the iterator on flex should be slower because it needs to do more calculations.
What could be the reason for this?
In fact, if I turn off the Compiled switch, it will work after 8 ms when caching. I thought that βcompiledβ would compile it when building a regex. If not, when is this done?
source share