# Fastest case sensitive equality test

**URL:** <https://forum.xojo.com/t/fastest-case-sensitive-equality-test/61119>\
**Category:** General\
**Created:** [February 11, 2021, 4:00am UTC](https://forum.xojo.com/t/fastest-case-sensitive-equality-test/61119 "2021-02-11T04:00:43Z")\
**Posts on this page:** 19\
**Page:** 2

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [February 11, 2021, 5:22pm UTC](https://forum.xojo.com/t/fastest-case-sensitive-equality-test/61119/21 "2021-02-11T17:22:34Z")

</div>

Off the top of my head:

```auto
var chars() as string = s.Split( "" )
var upperChars() as string = s.Uppercase.Split( "" )

for index as integer = 0 to chars.LastIndex
  if chars( index ).Asc = upperChars( index ).Asc then
    // uppercase
  else
    // lowercase
  end if
next

```

---

<div class="post-metadata">

**Author:** ![Martin\_T](https://forum.xojo.com/letter_avatar_proxy/v4/letter/m/a88e57/32.png) [@Martin\_T](https://forum.xojo.com/u/Martin_T)\
**Post date:** [February 11, 2021, 5:30pm UTC](https://forum.xojo.com/t/fastest-case-sensitive-equality-test/61119/22 "2021-02-11T17:30:38Z")

</div>

Thanks @Kem_Tekinay. Interesting, using

```auto
Private Function IsLowercase(value As String) As Boolean
  Return value.Asc = value.Lowercase.Asc
End Function

```

instead of

```auto
Private Function IsLowercase(value As String) As Boolean
  Var iResult As Integer = value.Compare(value.Lowercase, ComparisonOptions.CaseSensitive)
  Return iResult = 0
End Function

```

makes in my test case a difference of **20 ms**. Further suggestions for optimization?

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [February 11, 2021, 5:43pm UTC](https://forum.xojo.com/t/fastest-case-sensitive-equality-test/61119/23 "2021-02-11T17:43:40Z")

</div>

Note that s.Asc will return the code point for the first character only. Is that what you want?

Are you measuring the calling function too? If so, are you using Split the parse the characters?

---

<div class="post-metadata">

**Author:** ![Martin\_T](https://forum.xojo.com/letter_avatar_proxy/v4/letter/m/a88e57/32.png) [@Martin\_T](https://forum.xojo.com/u/Martin_T)\
**Post date:** [February 11, 2021, 5:51pm UTC](https://forum.xojo.com/t/fastest-case-sensitive-equality-test/61119/24 "2021-02-11T17:51:32Z")

</div>

It doesn’t matter in my case, because I call the function inside a loop for single characters:

```auto
For Each char As String In source.Characters
  If IsLowerCase(char) Then ...
Next

```

That’s why it works wonderfully in my case. I have also tested it with Cyrillic letters etc. .

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [February 11, 2021, 5:55pm UTC](https://forum.xojo.com/t/fastest-case-sensitive-equality-test/61119/25 "2021-02-11T17:55:18Z")

</div>

The only optimization I see is to inline the test as function calls have overhead.

Edit: But that affects readability.

---

<div class="post-metadata">

**Author:** ![Martin\_T](https://forum.xojo.com/letter_avatar_proxy/v4/letter/m/a88e57/32.png) [@Martin\_T](https://forum.xojo.com/u/Martin_T)\
**Post date:** [February 11, 2021, 5:57pm UTC](https://forum.xojo.com/t/fastest-case-sensitive-equality-test/61119/26 "2021-02-11T17:57:27Z")

</div>

Cool, thanks. However, this doesn’t make sense in my case, because I call the function in other methods as well and don’t want duplicate code 😉

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [February 11, 2021, 6:02pm UTC](https://forum.xojo.com/t/fastest-case-sensitive-equality-test/61119/27 "2021-02-11T18:02:25Z")

</div>

I agree.

But in thinking about it more, where speed is important, you are calling the Lowercase function for each character of the string instead of just once on the whole thing. If your string is 100 character, it probably doesn’t make a difference. If it’s 100k, you might feel that.

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [February 11, 2021, 6:11pm UTC](https://forum.xojo.com/t/fastest-case-sensitive-equality-test/61119/28 "2021-02-11T18:11:31Z")

</div>

In other words, instead of converting each character to lowercase before the comparison, convert the string to lowercase, then compare each character to the corresponding character of the original.

---

<div class="post-metadata">

**Author:** ![anon93744516](https://forum.xojo.com/letter_avatar_proxy/v4/letter/a/5e9695/32.png) [@anon93744516](https://forum.xojo.com/u/anon93744516)\
**Post date:** [May 9, 2021, 12:53am UTC](https://forum.xojo.com/t/fastest-case-sensitive-equality-test/61119/29 "2021-05-09T00:53:29Z")

</div>

> [@Christian\_Schmitz](#):
>
> What platforms?  
> You could also use [NSStringCompareMBS](https://www.monkeybreadsoftware.net/global-nsstringcomparembs.shtml) function on macOS and iOS to compare with various options.

After reading the [Leaking Locale and core.Local Objects?](https://forum.xojo.com/t/leaking-locale-and-core-local-objects/63130) 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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![Sam\_Rowlands](https://forum.xojo.com/user_avatar/forum.xojo.com/sam_rowlands/32/265_2.png) [@Sam\_Rowlands](https://forum.xojo.com/u/Sam_Rowlands)\
**Post date:** [May 9, 2021, 2:29am UTC](https://forum.xojo.com/t/fastest-case-sensitive-equality-test/61119/30 "2021-05-09T02:29:46Z")

</div>

There must be a way to efficiently use memory blocks for this. Albeit I can’t think of one at the moment. I use memoryblocks for case sensitive “select case”.

---

<div class="post-metadata">

**Author:** ![Markus\_Winter](https://forum.xojo.com/user_avatar/forum.xojo.com/markus_winter/32/144_2.png) [@Markus\_Winter](https://forum.xojo.com/u/Markus_Winter)\
**Post date:** [May 9, 2021, 4:49am UTC](https://forum.xojo.com/t/fastest-case-sensitive-equality-test/61119/31 "2021-05-09T04:49:34Z")

</div>

If you have to use MemoryBlocks for standard operations then Xojo is doing something wrong.

---

<div class="post-metadata">

**Author:** ![Christian\_Schmitz](https://forum.xojo.com/user_avatar/forum.xojo.com/christian_schmitz/32/158_2.png) [@Christian\_Schmitz](https://forum.xojo.com/u/Christian_Schmitz)\
**Post date:** [May 9, 2021, 7:57am UTC](https://forum.xojo.com/t/fastest-case-sensitive-equality-test/61119/32 "2021-05-09T07:57:02Z")

</div>

Thanks for comparison.

For **String.Compare** I made a feedback case [64647](http://feedback.xojo.com/case/64647) as it converts all Strings to Text and then does a compare, which makes it slower than it needs to be. And on macOS the compare may be with creating CFString internally (another copy in addition to text) to do the compare.

For **NSStringCompareMBS** similarly you have the overhead of a plugin function call, which is not efficient as it could be. (see [62010](http://feedback.xojo.com/case/62010)). And then our plugin will do CFString/NString comparison for you.

---

<div class="post-metadata">

**Author:** ![JensK](https://forum.xojo.com/user_avatar/forum.xojo.com/jensk/32/619_2.png) [@JensK](https://forum.xojo.com/u/JensK)\
**Post date:** [May 9, 2021, 7:57am UTC](https://forum.xojo.com/t/fastest-case-sensitive-equality-test/61119/33 "2021-05-09T07:57:38Z")

</div>

String.Compare may be slower than other methods, but in one project it was the only way for me to get a decent ordering of a ListBox containing “Umlaute”. (StrComp didn’t work properly.) I don’t know how else I could have it done so I am glad I have this option. 🤔

 ![Bildschirmfoto 2021-05-09 um 09.43.39](https://forum.xojo.com/uploads/default/original/2X/e/e1da3770f2302eaf2b46e761eec8446f21f94d87.png)

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [May 9, 2021, 11:31am UTC](https://forum.xojo.com/t/fastest-case-sensitive-equality-test/61119/34 "2021-05-09T11:31:07Z")

</div>

Unicode normalization issue?

---

<div class="post-metadata">

**Author:** ![JensK](https://forum.xojo.com/user_avatar/forum.xojo.com/jensk/32/619_2.png) [@JensK](https://forum.xojo.com/u/JensK)\
**Post date:** [May 9, 2021, 11:45am UTC](https://forum.xojo.com/t/fastest-case-sensitive-equality-test/61119/35 "2021-05-09T11:45:33Z")

</div>

I fear I don‘t know what that is. 🙈

---

<div class="post-metadata">

**Author:** ![Beatrix\_Willius](https://forum.xojo.com/user_avatar/forum.xojo.com/beatrix_willius/32/282_2.png) [@Beatrix\_Willius](https://forum.xojo.com/u/Beatrix_Willius)\
**Post date:** [May 9, 2021, 11:47am UTC](https://forum.xojo.com/t/fastest-case-sensitive-equality-test/61119/36 "2021-05-09T11:47:22Z")

</div>

You will learn the first time when strings that should be the same don’t match. I thought I was going crazy.

---

<div class="post-metadata">

**Author:** ![JensK](https://forum.xojo.com/user_avatar/forum.xojo.com/jensk/32/619_2.png) [@JensK](https://forum.xojo.com/u/JensK)\
**Post date:** [May 9, 2021, 11:52am UTC](https://forum.xojo.com/t/fastest-case-sensitive-equality-test/61119/37 "2021-05-09T11:52:06Z")

</div>

I will look into this as soon as it bugs me … (Fortunately I didn‘t have to deal with that sort of problems before.)

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [May 9, 2021, 12:04pm UTC](https://forum.xojo.com/t/fastest-case-sensitive-equality-test/61119/38 "2021-05-09T12:04:45Z")

</div>

> **[Unicode equivalence](https://en.wikipedia.org/wiki/Unicode_equivalence)**
>
> Unicode equivalence is the specification by the Unicode character encoding standard that some sequences of code points represent essentially the same character. This feature was introduced in the standard to allow compatibility with preexisting standard character sets, which often included similar or identical characters.
> Unicode provides two such notions, canonical equivalence and compatibility. Code point sequences that are defined as canonically equivalent are assumed to have the same appear...

In a nutshell, some characters can be represented in two different ways: one code point that represents the character, or a series of two or more code points.

To see this in action in Xojo, try this:

```auto
MessageBox "e" + &u0300

```

(I am greatly simplifying the issue here.)

Normalization is the process of getting all the character represented in the same way, either Composed (one code point) or Decomposed (two or more code points). Once you have achieved that consistency, things like sorts and searches will work properly.

My M\_String project include normalization code, as does the MBS plugins.

[http://www.mactechnologies.com/index.php?dowloads](http://www.mactechnologies.com/index.php?dowloads)

---

<div class="post-metadata">

**Author:** ![Christian\_Schmitz](https://forum.xojo.com/user_avatar/forum.xojo.com/christian_schmitz/32/158_2.png) [@Christian\_Schmitz](https://forum.xojo.com/u/Christian_Schmitz)\
**Post date:** [May 9, 2021, 4:59pm UTC](https://forum.xojo.com/t/fastest-case-sensitive-equality-test/61119/39 "2021-05-09T16:59:14Z")

</div>

That is why there are flags like

```
const NSDiacriticInsensitiveSearch = 128
const NSWidthInsensitiveSearch = 256
```

[Previous page](https://forum.xojo.com/t/fastest-case-sensitive-equality-test/61119.md?page=1)
