如何String.Contains工作? [英] How does String.Contains work?
问题描述
可能重复:结果
的什么算法使用的.Net在字符串搜索的模式?
我在我的程序,它从一个文件中获得的线循环。再有一个检查线路是否包含字符串
I have a loop in my program that gets a line from a file. Then there is a check to whether the line contains a string
if(line.Contains("String"))
{
//Do other stuff
}
有超过200万在该文件中的行,所以如果我可以连1/10毫秒加快速度那么这将节省我过去在每次运行时3分钟。
There are over 2 million rows in the file so if I can quicken the speed by even 1/10th millisecond then this would save me over 3 minutes on each run.
所以...一说线是1000个字符长,可以更快地寻找一个或长或短的字符串,或者它不能有所作为?
So... Say a line is 1000 chars long, is it quicker to look for a short or long string, or does it not make a difference?
line.Contains("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
或
line.Contains("ABCDEFG")
感谢您提前。
推荐答案
String.Contains()通过如下进System.Globalization.CompareInfo CLR和NLS支持子系统,我彻底地得到了一个曲折的路线丢失。这包含高度优化的代码令人印象深刻的PERF。做得更快的唯一方法是通过pinvoking标准的CRT函数wcsstr,在MSVCRT.DLL
String.Contains() follows a torturous route through System.Globalization.CompareInfo into the CLR and the NLS support sub-system where I thoroughly got lost. This contains highly optimized code with impressive perf. The only way to do it faster is through pinvoking the standard CRT function wcsstr, available in msvcrt.dll
[DllImport("msvcrt.dll", CharSet = CharSet.Unicode)]
private static extern IntPtr wcsstr(string toSearch, string toFind);
如果没有找到字符串,返回IntPtr.Zero。我做了一些测量,使用String.IndexOf(),而不是包含()来测试不同的字符串比较选项。所有的时间都在纳秒,寻找7个字符的60个字符的字符串的字符串。与串不存在这样最坏的情况下被测量。使用了最低时间为20个样品的
It returns IntPtr.Zero if the string wasn't found. I made some measurements, using String.IndexOf() instead of Contains() to test the various string comparison options. All times are in nanoseconds, searching for a string of 7 characters in a string of 60 characters. With the string not present so worst case is measured. The lowest time out of a sample of 20 was used:
StringComparison.Ordinal (same as Contains) : 245 nanoseconds
StringComparison.OrdinalIgnoreCase : 327
StringComparison.InvariantCulture : 251
StringComparison.InvariantCultureIgnoreCase : 327
StringComparison.CurrentCulture : 275
StringComparison.CurrentCultureIgnoreCase : 340
wcsstr : 213
非常可观的数字,并看齐,与你所期望的这些功能所需要的。该wcsstr()函数相同种类序号比较为的String.Compare()的。它是更快只有13%,假设真实PERF的是不可能接近这些测量由于CPU高速缓存局部性的效果的统计学显着的改善。我只能说你要一样快,你可以期望。无论是从wcsstr的略有改善是值得的,是由你。
Very impressive numbers and on par with what you'd expect these functions to require. The wcsstr() function does the same kind of ordinal comparison as String.Compare(). It is only 13% faster, a statistically insignificant improvement given that real perf is not likely to approach these measurements due to the effects of CPU cache locality. I can only conclude that you're going about as fast as you can expect. Whether the slight improvement from wcsstr is worth it is up to you.
这篇关于如何String.Contains工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!