[英]The fastest way to count string occurences in another string in c#
I have to count string occurences in one very loooong string (about 30mb in plain text) I use the following code for now: int count = new Regex(Regex.Escape(stringThatIamLookingFor)).Matches(stringToSearchIn).Count;
我必须计算一个很长的字符串中的字符串出现次数(纯文本约30mb),现在我使用以下代码: int count = new Regex(Regex.Escape(stringThatIamLookingFor)).Matches(stringToSearchIn).Count;
but is is too slow. 但是太慢了。 It takes about 3 minutes on i7 and 16gb ram. 在i7和16GB内存上大约需要3分钟。 The example data is: 示例数据为:
43.996442,-31.768039
43.996432,-31.768039
43.996432,-31.768049
43.996422,-31.768049
43.996422,-31.768059
I want to count (for example) .7
Is there faster way than regeex? 我想算一下(例如) .7
有没有比regeex更快的方法?
The fastest function so far is: (I need to check only two chars.) 到目前为止,最快的功能是:(我只需要检查两个字符。)
public int countOccurences2(string source, char charOne, char charTwo)
{
int count = 0;
for (int i=0; i<=source.Length-1; i=i+2)
if (source[i] == charOne && source[i + 1] == charTwo) { count++; }
return count;
}
from this question: How would you count occurrences of a string within a string? 来自这个问题: 如何计算一个字符串中一个字符串的出现次数?
the following code seems the most performing: 以下代码似乎是性能最高的:
int count = 0, n = 0;
if(substring != "")
{
while ((n = source.IndexOf(substring, n, StringComparison.InvariantCulture)) != -1)
{
n += substring.Length;
++count;
}
}
solution provided by Richard Watson in the mentioned question Richard Watson在提到的问题中提供的解决方案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.