Fastest case sensitive equality test

After reading the Leaking Locale and core.Local Objects? thread. I started thinking about string comparison performance and found the current thread.

And after reading through here, I was interested in how NSStringCompareMBS would preform, if included as another test case (based on the code in the original post).

So I added the following two test runs:

dblSeconds = Xojo.Core.Date.Now.SecondsFrom1970
intRounds = 0
Do Until intRounds = 10000
  intRounds = intRounds + 1
  If NSStringCompareMBS(strTest1, strTest2, 0) <> 0 Then
    Break
  End If
Loop
strMessage = strMessage + Chr(13) + Str(Xojo.Core.Date.Now.SecondsFrom1970 - dblSeconds) + " seconds for 10,000 NSStringCompareMBS case-sensitive string comparisons"

dblSeconds = Xojo.Core.Date.Now.SecondsFrom1970
intRounds = 0
Do Until intRounds = 10000
  intRounds = intRounds + 1
  If NSStringCompareMBS(strTest1, strTest2, 1) <> 0 Then
    Break
  End If
Loop
strMessage = strMessage + Chr(13) + Str(Xojo.Core.Date.Now.SecondsFrom1970 - dblSeconds) + " seconds for 10,000 NSStringCompareMBS case-insensitive string comparisons"

And got the following results:

0.0206921 seconds for 10,000 String.Compare
0.0049689 seconds for 10,000 HexEncoding comparisons
0.5892110 seconds for 10,000 Hashing comparisons
0.0012398 seconds for 10,000 case-insensitive string comparisons
0.0022290 seconds for 10,000 NSStringCompareMBS case-sensitive string comparisons
0.0021710 seconds for 10,000 NSStringCompareMBS case-insensitive string comparisons

Note: I included the results of all tests, because I’m using Xojo 2021r1.1 on a 2018 Mac Mini (10.15.7) with 3.2 GHz 6-Core Intel Core i7 & 32Gb RAM.

My conclusion was, for case-insensitive string comparisons use the = or <> operators. And for case-sensitive matches, use NSStringCompareMBS - if available to you and appropriate.

I hope that is useful to someone. Thanks.

2 Likes