How can C#'s string.IndexOf perform so fast, 10 times faster than ordinary for loop find?

后端 未结 5 1550
野趣味
野趣味 2021-02-19 03:41

I have a very long string (60MB in size) in which I need to find how many pairs of \'<\' and \'>\' are in there.


I have first tried my own way:

5条回答
  •  [愿得一人]
    2021-02-19 04:17

    Are you running your timings from within Visual Studio? If so, your code would run significantly slower for that reason alone.

    Aside from that, you are, to some degree, comparing apples and oranges. The two algorithms work in a different way.

    The IndexOf version alternates between looking for an opening bracket only and a closing bracket only. Your code goes through the whole string and keeps a status flag that indicates whether it is looking for an opening or a closing bracket. This takes more work and is expected to be slower.

    Here's some code that does the comparison the same way as your IndexOf method.

    int match3 = 0;
    for (int j = 0; j < html.Length; j++) {
        if (html[j] == '<') {
            for (; j < html.Length; j++)
                if (html[j] == '>')
                    match3++;
        }
    }
    

    In my tests this is actually about 3 times faster than the IndexOf method. The reason? Strings are actually not quite as simple as sequences of individual characters. There are markers, accents, etc. String.IndexOf handles all that extra complexity properly, but it comes at a cost.

提交回复
热议问题