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

后端 未结 5 1547
野趣味
野趣味 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:13

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

提交回复
热议问题