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 in order to search fast in large collections. I think there are only two methods: Compare
and CompareOrdinal
. What's the fastest?
Additionally, is there is a faster method for those string-comparisons?
I Checked both the string.Compare and string.CompareOrdinal using stop watch
In case 1 Average elapsed timing was 00:00:00.0000030 In case 2 Average elapsed timing was 00:00:00.0000086
I tried with different Equal and not equal combinations of string and found that every time CompareOrdinal is faster than only compare..
That is my own observation..you can also try just put two buttons on a form and copy paste this code in regrading event..
This is quite an old question, but since I found it others might as well.
In researching this topic a bit further, I came upon an interesting blog post that compares all methods for string comparison. Probably not highly scientific but still a good housenumber.
Thanks to this article I started using string.CompareOrdinal in a scenario where I had to find out if one string was in a list of 170.000 other strings and doing this 1600 times in a row. string.CompareOrdinal made it almost 50% faster compared to string.Equals
This might be useful to someone, but changing one line of my code brought the unit testing of my method down from 140ms to 1ms!
Original
Unit test: 140ms
New
Unit test: 1ms
Unit Test (NUnit)
Interestingly StringsMatch_OnlyString1NullOrEmpty_ReturnFalse() was the only unit test that took 140ms for the StringsMatch method. StringsMatch_AllParamsNullOrEmpty_ReturnTrue() was always 1ms and StringsMatch_OnlyString2NullOrEmpty_ReturnFalse() always <1ms.
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:
C#:
This could be dependant on your own strings but its seems to have worked well for me.
I'm assuming you want a less-than/equal/greater-than comparison rather than just equality; equality is a slightly different topic, although the principles are basically the same. If you're actually only searching for presence in something like a
SortedList
, I'd consider using aDictionary<string, XXX>
instead - do you really need all that sorting?String.CompareOrdinal
, or using an overload ofString.Compare
which allows the comparison to be provided, and specifying an ordinal (case-sensitive) comparison, e.g.String.Compare(x, y, StringComparison.Ordinal)
will be the fastest.Basically an ordinal comparison just needs to walk the two strings, character by character, until it finds a difference. If it doesn't find any differences, and the lengths are the same, the result is 0. If it doesn't find any differences but the lengths aren't the same, the longer string is deemed "larger". If it does find a difference, it can immediately work out which is deemed "larger" based on which character is "larger" in ordinal terms.
To put is another way: it's like doing the obvious comparison between two
char[]
values.Culture-sensitive comparisons have to perform all kinds of tortuous feats, depending on the precise culture you use. For an example of this, see this question. It's pretty clear that having more complex rules to follow can make this slower.
Fastest is interned strings with reference equality test, but you only get equality testing and it's at the heavy expense of memory - so expensive that it's almost never the recommended course.
Past that, a case-sensitive ordinal test will be the fastest, and this method is absolutely recommended for non-culture-specific strings. Case-sensitive is faster if it works for your use case.
Source