VBspeed / Language / Same String?
VBspeed © 2000-10, updated: 13-Aug-2004
Same String?


Same String?
How do you test whether two strings are identical?
You probably use the equal operator:  If String1 = String2 Then ...
Nothing could be easier, but is it fast?
Code
EqualOperator fRet = (sDum1 = sDum2)
StrComp fRet = (StrComp(sDum1, sDum2, vbBinaryCompare) = 0)
InStrB
' by Donald, donald@xbeat.net, 20001011, rev 001 20040813
If LenB(sDum1) = LenB(sDum2) Then
  If LenB(sDum1) = 0 Then
    fRet = True
  Else
    fRet = (InStrB(1, sDum1, sDum2, vbBinaryCompare) <> 0)
  End If
End If
lstrcmp Public Declare Function lstrcmp Lib "kernel32" Alias "lstrcmpA" (ByVal lpString1 As String, ByVal lpString2 As String) As Long
fRet = (lstrcmp(sDum1, sDum2) = 0)
IsSameString01
fRet = IsSameString01(sDum1, sDum2)

Public Function IsSameString01(String1 As String, _
                               String2 As String) As Boolean
  ' by Donald, donald@xbeat.net, 20001011, rev 001 20040813
  If LenB(String1) = LenB(String2) Then
    If LenB(String1) = 0 Then
      IsSameString01 = True
    Else
      IsSameString01 = (InStrB(1, String1, String2, vbBinaryCompare) <> 0)
    End If
  End If
End Function
IsSameString02 IsSameString02 is wrapped in a class. Cinemascope here, preview here:

Calls
1both strings 1000 chars, all chars equal
2both strings 1000 chars, first char differs
3both strings 1000 chars, last char differs
41000 vs. 999 chars, all chars equal
 VB5 Charts
CodeAuthorDopingNotes
EqualOperator Donald  
StrComp Donald  
InStrB Donald  
lstrcmp JasonAPI 
IsSameString01 Donald  
IsSameString02 ChrisAPI 
Call 1
41.3012µs
51.3112µs
21.029µs
618.36166µs
31.029µs
11.009µs
Call 2
51.270.304µs
41.220.293µs
21.030.247µs
6607.34145.770µs
31.170.281µs
11.000.240µs
Call 3
51.3711.929µs
41.3611.888µs
21.129.727µs
619.03166.008µs
31.129.761µs
11.008.723µs
Call 4
5158.1811.815µs
4157.8711.791µs
11.000.075µs
62,222.27165.984µs
21.500.112µs
32.150.161µs
 VB6 Charts
CodeAuthorDopingNotes
EqualOperator Donald  
StrComp Donald  
InStrB Donald  
lstrcmp JasonAPI 
IsSameString01 Donald  
IsSameString02 ChrisAPI 
Call 1
21.3211.809µs
31.3211.822µs
42.6123.367µs
618.57166.165µs
52.6223.403µs
11.008.947µs
Call 2
41.370.305µs
51.470.327µs
21.120.250µs
6654.20145.559µs
31.270.282µs
11.000.222µs
Call 3
31.3411.974µs
21.3411.971µs
42.6223.476µs
618.55166.195µs
52.6323.562µs
11.008.961µs
Call 4
4186.3911.801µs
5186.5311.809µs
11.000.063µs
62,613.35165.451µs
21.730.109µs
32.280.144µs
Notes & Conclusions
  • The equal operator is not very fast (at string comparison)! Looking at the numbers, you see that it compares strings left to right and that it's smart enough to quit comparing when it spots the first difference. However, it's too dumb to first do the most obvious test: comparing the lengths of the strings! Call 4 reveals the catastrophic results of this.
  • Looking at Call 1 and Call 3 (VB5 Charts), you see that the equal operator is so bad that it even loses against a function call to IsSameString01!
    This, however, is no more the case under VB6: sadly enough, the InStr/InStrB function got much slower under VB6 when the scanning has to go a long way (Call 1 and Call 3)!
  • The StrComp function does not make a difference here. Note, however, that it's a bit faster than the equal operator with shorter strings!
  • By the way, I'm using LenB and InStrB because they are a bit faster than Len and InStr.
  • The lstrcmp API has some problem here. Can anybody tell me what's going on? Yes: "When you Declare an API call with As String parameters, VB converts the strings from Unicode to ANSI before it makes the call. This explains lstrcmp's poor performance." (Aidan Corey, aidan.corey@pas.co.uk). (See also Appleman Win32API, p. 44)
The bottom line is:
  1. Just forget about the equal operator and use IsSameString02 to compare strings of the same length, and InStrB if your strings are likely to differ in length.
Got comments? How to read all those numbers

top




VBspeed © 2000-10 by Donald Lessau