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

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

    The only thing that comes to my mind is actual implementation of IndexOf iniside string class, that call

    callvirt    System.String.IndexOf
    

    which, if we use a power of reflector (as much as it possible) ends up into the

    CompareInfo.IndexOf(..)
    

    call, which instead use super fast windows native function FindNLSString:

    enter image description here

提交回复
热议问题