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:
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.