What is the fastest (built-in) comparison for string-types in C#

前端 未结 8 1516
南方客
南方客 2021-02-05 02:09

What is the fastest built-in comparison-method for string-types in C#? I don\'t mind about the typographical/semantical meaning: the aim is to use the comparator in sorted lists

8条回答
  •  无人及你
    2021-02-05 02:33

    I just noticed a 50% performance increase in my own code by comparing string lengths first and if equal then using the string.compare methods. So in a loop I have:

    VB:

    If strA.length = strB.length then
       if string.compare(strA,strB,true) = 0 then
          TheyAreEqual
       End if
    End if
    

    C#:

    if(strA.Length == strB.Length)
    {
       if(string.Compare(strA,strB,true) == 0)
       {
           //they are equal
       }
    }
    

    This could be dependant on your own strings but its seems to have worked well for me.

提交回复
热议问题