这只是一个问题,以满足我的好奇心。 但对我来说这很有趣。为什么缓存的Regexp超越编译好的版本?
我写了这个简单的基准。它以几千次的随机顺序调用Regexp执行的3个变体:
基本上,我使用相同的模式,但以不同的方式。
您的普通方式没有任何
RegexOptions
。从.NET 2.0开始,这些不会被缓存。但应该“缓存”,因为它保存在一个非常全球的范围内,而不是重置。随着
RegexOptions.Compiled
随着到静态
Regex.Match(pattern, input)
调用它不会在.NET 2.0
这里获取缓存是代码:
static List<string> Strings = new List<string>();
static string pattern = ".*_([0-9]+)\\.([^\\.])$";
static Regex Rex = new Regex(pattern);
static Regex RexCompiled = new Regex(pattern, RegexOptions.Compiled);
static Random Rand = new Random(123);
static Stopwatch S1 = new Stopwatch();
static Stopwatch S2 = new Stopwatch();
static Stopwatch S3 = new Stopwatch();
static void Main()
{
int k = 0;
int c = 0;
int c1 = 0;
int c2 = 0;
int c3 = 0;
for (int i = 0; i < 50; i++)
{
Strings.Add("file_" + Rand.Next().ToString() + ".ext");
}
int m = 10000;
for (int j = 0; j < m; j++)
{
c = Rand.Next(1, 4);
if (c == 1)
{
c1++;
k = 0;
S1.Start();
foreach (var item in Strings)
{
var m1 = Rex.Match(item);
if (m1.Success) { k++; };
}
S1.Stop();
}
else if (c == 2)
{
c2++;
k = 0;
S2.Start();
foreach (var item in Strings)
{
var m2 = RexCompiled.Match(item);
if (m2.Success) { k++; };
}
S2.Stop();
}
else if (c == 3)
{
c3++;
k = 0;
S3.Start();
foreach (var item in Strings)
{
var m3 = Regex.Match(item, pattern);
if (m3.Success) { k++; };
}
S3.Stop();
}
}
Console.WriteLine("c: {0}", c1);
Console.WriteLine("Total milliseconds: " + (S1.Elapsed.TotalMilliseconds).ToString());
Console.WriteLine("Adjusted milliseconds: " + (S1.Elapsed.TotalMilliseconds).ToString());
Console.WriteLine("c: {0}", c2);
Console.WriteLine("Total milliseconds: " + (S2.Elapsed.TotalMilliseconds).ToString());
Console.WriteLine("Adjusted milliseconds: " + (S2.Elapsed.TotalMilliseconds*((float)c2/(float)c1)).ToString());
Console.WriteLine("c: {0}", c3);
Console.WriteLine("Total milliseconds: " + (S3.Elapsed.TotalMilliseconds).ToString());
Console.WriteLine("Adjusted milliseconds: " + (S3.Elapsed.TotalMilliseconds*((float)c3/(float)c1)).ToString());
}
每次我叫它结果如下:
Not compiled and not automatically cached: Total milliseconds: 6185,2704 Adjusted milliseconds: 6185,2704 Compiled and not automatically cached: Total milliseconds: 2562,2519 Adjusted milliseconds: 2551,56949184038 Not compiled and automatically cached: Total milliseconds: 2378,823 Adjusted milliseconds: 2336,3187176891
所以你有它。不多,但约7-8%的差异。
这不是唯一的秘密。我无法解释为什么第一种方法会慢得多,因为它不会被重新评估,而是被保存在一个全局静态变量中。
顺便说一句,这是在.net 3.5和Mono 2.2上表现完全一样。在Windows上。
那么,任何想法,为什么编译的变体甚至会落后?
EDIT1:
Not compiled and not automatically cached: Total milliseconds: 6456,5711 Adjusted milliseconds: 6456,5711 Compiled and not automatically cached: Total milliseconds: 2668,9028 Adjusted milliseconds: 2657,77574842168 Not compiled and automatically cached: Total milliseconds: 6637,5472 Adjusted milliseconds: 6518,94897724836
还几乎废止所有的其他问题还有:
固定码结果现在这个样子了。
感谢您的答案。
谢谢你的解释。 在我的情况下,最初的步骤似乎不会导致很多成本(请参阅新结果)。 我在发布之前阅读了Jeff Atwood的帖子。所以我知道这些缺点。在我的情况下,编译选项会有所帮助,虽然在标准用例中没有那么多。 – user51710 2009-01-09 15:15:52
** Jeff Atwood的帖子已经移到:[编译或不编译*(2005年3月3日)*](http://blog.codinghorror.com/to-compile-or-not-to-compile/) – DavidRR 2014-04-10 17:39:43