在DOTNET框架4字符串比较 [英] String comparison in dotnet framework 4
问题描述
我会解释我的问题(请原谅我的英语不好),我有一个.NET的exe在处理每毫秒是非常重要的。
I will explain my problem(excuse my bad English), I have a .NET exe in which every milliseconds of processing is very important.
这个程序做大量的字符串比较的(大部分是 string1.IndexOf(字符串2,StringComparison.OrdinalIgnoreCase)
)。
This program does lots of string comparison (most of it is string1.IndexOf(string2, StringComparison.OrdinalIgnoreCase)
).
当我切换到框架4,我的节目时间是两倍了。
When i switch to framework 4, my program time is twice than before.
我搜索了解释,我发现功能的IndexOf(S,OrdinalIgnoreCase)
是框架4慢得多(我做测试用一个简单的控制台应用程序,并在循环时间为30毫秒在3.5和210ms的4.0 ???)。但是,在当前的文化比较快的框架4比3.5。
I searched for explanation and I found that the function IndexOf(s, OrdinalIgnoreCase)
is much slower in framework 4 (I did test with a simple console application and in a loop the time was 30ms in 3.5 and 210ms in 4.0 ???). But the comparison in current culture is quicker in framework 4 than 3.5.
下面它的code样品使用:
Here it's a sample of code I use :
int iMax = 100000;
String str = "Mozilla/5.0+(Windows;+U;+Windows+NT+5.1;+fr;+rv:1.9.0.1)+Gecko/2008070208+Firefox/3.0.1";
Stopwatch sw = new Stopwatch();
sw.Start();
StringComparison s = StringComparison.OrdinalIgnoreCase;
for(int i = 1;i<iMax;i++)
{
str.IndexOf("windows", s);
}
sw.Stop();
Console.WriteLine(sw.ElapsedMilliseconds);
Console.Read();
我的问题是:
My questions are :
-
有没有人注意到了同样的问题?
Has anyone noticed the same problem?
有人对这种变化的解释?
Someone have an explanation on this change?
有没有解决办法绕过这个问题?
Is there a solution to bypass the problem?
感谢。
推荐答案
好吧,我有我的问题的一个回应。
Ok i have a response of one of my question.
使用反射镜我可以看到框架2和4的区别,并解释我的perforamnce问题。
With reflector i can see the difference between framework 2 and 4 and that explain my perforamnce issue.
public int IndexOf(string value, int startIndex, int count, StringComparison comparisonType)
{
if (value == null)
{
throw new ArgumentNullException("value");
}
if ((startIndex < 0) || (startIndex > this.Length))
{
throw new ArgumentOutOfRangeException("startIndex", Environment.GetResourceString("ArgumentOutOfRange_Index"));
}
if ((count < 0) || (startIndex > (this.Length - count)))
{
throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_Count"));
}
switch (comparisonType)
{
case StringComparison.CurrentCulture:
return CultureInfo.CurrentCulture.CompareInfo.IndexOf(this, value, startIndex, count, CompareOptions.None);
case StringComparison.CurrentCultureIgnoreCase:
return CultureInfo.CurrentCulture.CompareInfo.IndexOf(this, value, startIndex, count, CompareOptions.IgnoreCase);
case StringComparison.InvariantCulture:
return CultureInfo.InvariantCulture.CompareInfo.IndexOf(this, value, startIndex, count, CompareOptions.None);
case StringComparison.InvariantCultureIgnoreCase:
return CultureInfo.InvariantCulture.CompareInfo.IndexOf(this, value, startIndex, count, CompareOptions.IgnoreCase);
case StringComparison.Ordinal:
return CultureInfo.InvariantCulture.CompareInfo.IndexOf(this, value, startIndex, count, CompareOptions.Ordinal);
case StringComparison.OrdinalIgnoreCase:
return TextInfo.IndexOfStringOrdinalIgnoreCase(this, value, startIndex, count);
}
throw new ArgumentException(Environment.GetResourceString("NotSupported_StringComparison"), "comparisonType");
}
这是函数的基础code中的2框架的IndexOf(间4并没有什么区别2)
This is the base code of function IndexOf of the 2 framework (no difference between 4 and 2)
但在功能TextInfo.IndexOfStringOrdinalIgnoreCase存在差异:
But in the function TextInfo.IndexOfStringOrdinalIgnoreCase there are differences :
框架2:
internal static unsafe int IndexOfStringOrdinalIgnoreCase(string source, string value, int startIndex, int count)
{
if (source == null)
{
throw new ArgumentNullException("source");
}
return nativeIndexOfStringOrdinalIgnoreCase(InvariantNativeTextInfo, source, value, startIndex, count);
}
Framework 4的:
Framework 4 :
internal static int IndexOfStringOrdinalIgnoreCase(string source, string value, int startIndex, int count)
{
if ((source.Length == 0) && (value.Length == 0))
{
return 0;
}
int num = startIndex + count;
int num2 = num - value.Length;
while (startIndex <= num2)
{
if (CompareOrdinalIgnoreCaseEx(source, startIndex, value, 0, value.Length, value.Length) == 0)
{
return startIndex;
}
startIndex++;
}
return -1;
}
主要算法框架发生了改变2呼叫是nativeDll已删除框架4。 其良好的知道
The main algorithm has changed in framework 2 the call is a nativeDll that has been removed of framework 4. Its good to know
这篇关于在DOTNET框架4字符串比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!