The fastest way to count lines in another line in C #

I need to count the lines in one very free line (about 30 MB in text form) I use the following code: int count = new Regex(Regex.Escape(stringThatIamLookingFor)).Matches(stringToSearchIn).Count; but it is too slow. It takes about 3 minutes on i7 and 16gb RAM. Examples are:

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 a faster way than regeex?

ok, solved

The fastest function so far: (I need to check only two characters.)

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;
    }
+1
source share
1 answer

from this question: How would you count occurrences of a string in a string?

The following code looks the most efficient:

int count = 0, n = 0;

if(substring != "")
{
    while ((n = source.IndexOf(substring, n, StringComparison.InvariantCulture)) != -1)
    {
        n += substring.Length;
        ++count;
    }
}

decision provided by Richard Watson on this issue

+3
source

Source: https://habr.com/ru/post/1616599/


All Articles