In Is That String Really Empty? I mentioned that Empty returns the same response as Len when checking variable- and fixed-length strings. Let's test which one is faster.
I am going to assume that Len is faster. To make things simpler I limited my tests to variable-length strings only.
The Test Procedure
As in previous posts, I wrote a simple non-scientific loop to casually test the speed of both functions.
Sub TestLenEmpty()
Dim i As Long
Dim str1 As String
Dim starttimeLen As Single
Dim endtimeLen As Single
Dim starttimeEmpty As Single
Dim endtimeEmpty As Single
Dim msg As String
Dim hasValue As Boolean
Const numberOfLoops As Long = 100000000
str1 = "Hello World"
' use Len
starttimeLen = Timer
For i = 1 To numberOfLoops
hasValue = (Len(str1) > 0)
Next i
endtimeLen = Timer
' use Empty
starttimeEmpty = Timer
For i = 1 To numberOfLoops
hasValue = (str1 = Empty)
Next i
endtimeEmpty = Timer
msg = "Number of iterations: " & numberOfLoops & vbCrLf
msg = msg & "Using Len: " & _
Format(endtimeLen - starttimeLen, "#.###") & " seconds" & vbCrLf
msg = msg & "Using Empty: " & _
Format(endtimeEmpty - starttimeEmpty, "#.###") & " seconds" & vbCrLf
MsgBox msg
End Sub
According to Excel's Visual Basic Help, Empty is a Variant subtype, so I expect some implicit type conversion here when comparing string values to Empty. What I didn't expect was the dramatic difference in speed.
Results
At 100,000 loop iterations, the difference is minimal:
At 1,000,000 iterations, both loops are still very quick, but starting to separate:
At 10,000,000 iterations it takes a full second longer for the second loop:
Finally, at 100 million iterations, the difference is striking: a full eleven seconds longer!
Conclusion
There is simply no substitute for Len (no pun intended). It is the fastest method for checking string length (and therefore, whether strings are empty), and it scales well. I never really used Empty anyway, and I don't plan on starting now.
I'm getting at 10,000,000 iterations:
Len>: 0.640625
Len: 0.671875
IsEmpty(): 0.296875
LenB>: 0.40625
LenB: 0.46875
Where > means I did a comparison with zero and w/o that means I just let VBA do the comparison. IsEmpty is the function. I think I might switch to IsEmpty, it's even faster than LenB! Amazing.
I take that back. IsEmpty gives the wrong answer if you start using the string, I guess I would need to set my strings to Empty everytime if I wanted to use it. Hmm, I wonder if setting a string to Empty (is that even possible?) compared to "" is faster.
It makes sense that Len is faster. I read somewhere that VB strings are saved in a compound variable, containing both the length and the characters. Len just extracts the numerical part of the variable.
Jon,
Yeah they're BSTR's under the covers, which store the length of the allocation before the character bytes. That being said, searching for a null terminator in a char array is not really painful, and there is something (barely) to be said for an extra allocation of 2 bytes per string, not to mention you cap the string length (32767).
If I remember correctly this length prefix was also used in delphi.
BTW, If you havent ever looked at it, check out the ridiculous optimizations they sluethed out in the c standard library implementation of strlen: http://www.stdlib.net/~colmmacc/strlen.c.html