0

I only learned of Option Compare Text recently so I wrote a little test:

Dim sw As New Stopwatch()
Dim w As Boolean = False
sw.Reset()
sw.Start()
Dim A As String = "hello, world"
For o As Integer = 1 To 1000000
    If A = "HELLO, world" Then
        w = True
    End If
Next o
sw.Stop()
Console.WriteLine(sw.Elapsed.TotalMilliseconds & vbTab & w.ToString)

This runs in about 6 milliseconds without that option, and about 450 with it on.

I'm not entirely surprised that it takes longer, but the magnitude is surprising. For instance:

If A.Equals("HELLO, world", StringComparison.CurrentCultureIgnoreCase) Then

...takes 135 millis.

Can anyone explain why it takes so much longer to use this option and = compared to the Equals method?

4
  • 2
    A quick test produces 7ms with compare binary and 150ms with compare text, which matches the StringComparison.CurrentCultureIgnoreCase. Commented Jul 24, 2024 at 15:54
  • Can't duplicate this, I'm still getting much longer compares on my machine. Commented Jul 24, 2024 at 16:03
  • Yeah, just ran it another 10 times both ways, and = is 3x slower that .Equals every time. Version issue maybe? Commented Jul 24, 2024 at 16:10
  • 1
    Probably ought to look at the IL generated in each case. Commented Jul 25, 2024 at 9:28

1 Answer 1

0

Well, one example does a high-speed CPU binary compare, and the other has to convert all characters to lower (or upper???) case, and then do a string compare.

I can't imagine anyone thinking that a raw binary compare that does NOT do any kind of string conversion to ignore case runs faster then code that has to covert the two strings to a common case, has to compare strings in place of raw binary data?

So, not really much of a surprise here, since one has to convert BOTH strings, and do a string compare.

The other example does not do string conversion, does not have to copy the strings (both of them) to a new location (for the case conversion), and then has to do a whole new string compare based on two whole brand-new values in memory, and in a whole new memory location! And all this has to occur not only on a copy of the two values, but the two values BOTH require a conversion to a common case!

So, not only is the case of the two strings require conversion, but that means a copy of the two strings has to occur also! And that means the code has to request and allocate two whole new memory locations.

Given the boatloads of extra memory, and extra processing here, and that of having to copy the two values, make a request for more and a new memory location, and then having to convert the two strings to a common case format? And THEN do a compare?

I get 4.8 vs 279. So, yes, it is about 58 times slower, and the above explains why!

So, not only is the CPU forced to do a string compare (as opposed to a faster binary compare), but a copy of the two values is required, and that means then two requests for two new memory locations has to occur. This will halt the CPU while the OS is now forced to get get and allocate some memory (which now brings the memory controllers into play). Then the two strings must be copied to the new location. Then after the copy of the two strings has occurred, then a case conversion (byte by byte, one character at a time!) for each string has to occur, and then and only then can the compare occur. So, yes, a boatload of extra work has to occur here.

Sign up to request clarification or add additional context in comments.

2 Comments

That's does not appear to be how the case insensitive Equals works. It appears to work as one might imagine, byThere are no copies, and just to be sure I did a million compares using the two methods and they did not show any difference in memory use in the profiler. I strongly suspect ta
Well, I doubt that a copy of the 2 strings is going to take up any noticiable amount of memory, and I doubt thus 2 small strings would show up as much as anything. I mean, the copy(s) are disposed of. At the end of the day, a COPY MUST be made for the compare, and it may well be JUST an expression of one byte, but then again, that just proves my whole point - in one case you can compare 2 locations in memory as binary, and in the other, each character has to be processed into a common case before the compare..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.