I only learned of Option Compare Text
recently so I wrote a little test:
<code>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)
</code>
<code>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)
</code>
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:
<code>If A.Equals("HELLO, world", StringComparison.CurrentCultureIgnoreCase) Then
</code>
<code>If A.Equals("HELLO, world", StringComparison.CurrentCultureIgnoreCase) Then
</code>
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?