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:
It's a bit of a fallacy to directly compare your managed implementation and the String.IndexOf
method. The implementation of IndexOf
is largely native code and hence has a different set of performance characteristics than your managed implementation. In particular native code avoid the type safety checks, and their corresponding overhead, which injected by the CLR in the managed algorithm.
One example is the use of the array index
char c = html[j];
In managed code this statement will both verify that j
is a valid index into the array html
and then return the value. The native code equivalent simply returns a memory offset with no additional checking necessary. This lack of a check gives native code an inherent advantage here because it can avoid an additional branch instruction on every iteration of the loop.
Note that this advantage is not absolute. The JIT could very well examine this loop and decide that j
could never be an invalid index and elide the checks in the generated native code (and it does do this in some cases IIRC).