开发者

Why is caching causing my code to run slower?

Non-cached:

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); // 76 ms

Cached

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); // 263 ms

I don't know why it's running slower when I pre-compile all the regexes. Not to mention the iterator on flex should be slower too, because it needs to do more calculations.

What could be causing 开发者_如何学Cthis?


Actually, if I take off the Compiled switch it runs in 8 ms when cached. I thought "compiled" would compile it upon construction of the regex. If not, when does it do so?


Regex's are in fact cached not just on first use, but upon construction (taking a look at the 4.0 code in reflector, it may not be precisely so in other frameworks).

As so, the big differences here are:

  1. There's some trivial string concatenation in the latter that isn't in the former, along with the overhead of construction outside of the compilation of the Regex.
  2. There's a different collection being iterated through in the latter than in the former.

It's not clear what sort of collection flex is. If it's not a dictionary, then I wouldn't be at all surprised by this, as dictionaries aren't terrribly fast at enumeration and hence most other collections will beat it.

This aside, it really isn't a case of caching in the latter, since it's caching something that's already going to be retrieved from an in-memory cache, so there's no reason to suspect the latter would be any faster.


The problem is with the RegexOptions.Compiled flag. That actually makes it run a lot slower. Jeff sort of explains this in his blog. Without this flag, the cached version is much faster.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜