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