Hope you had a fun New Year holiday!
Continuing with the series of tests I've been posting, another function I found is LenB. I don't know much about it, but it supposedly returns the length of a string, the same as Len.
According to Excel's Visual Basic Help, LenB
Returns a Long containing the number of characters in a string or the number of bytes required to store a variable. Instead of returning the number of characters in a string, LenB returns the number of bytes used to represent that string.
In my informal tests, LenB seems to return double the number of characters in a string. So Len("Hello World") = 11 and LenB("Hello World") = 22. This will come up later.
My assumption would be that checking the number of bytes should somehow be faster, but I really don't know.
The Test Procedure
This procedure tests the speed of both Len and LenB using my standard informal method of testing.
Sub TestLenB()
Dim i As Long, j As Long
Dim str1 As String
Dim starttimeLen As Single
Dim endtimeLen As Single
Dim starttimeLenB As Single
Dim endtimeLenB As Single
Dim msg As String
Const numberOfLoops As Long = 1000000
str1 = "Hello World"
' use Len
starttimeLen = Timer
For i = 1 To numberOfLoops
j = Len(str1)
Next i
endtimeLen = Timer
' use LenB
starttimeLenB = Timer
For i = 1 To numberOfLoops
j = LenB(str1) \ 2
Next i
endtimeLenB = Timer
msg = "Number of iterations: " & numberOfLoops & vbCrLf
msg = msg & "Using Len: " & _
Format(endtimeLen - starttimeLen, "#.###") & " seconds" & vbCrLf
msg = msg & "Using LenB: " & _
Format(endtimeLenB - starttimeLenB, "#.###") & " seconds"
MsgBox msg
End Sub
I used integer division instead of floating-point division with LenB because it was faster. Plus, returning just the value of LenB didn't seem equivalent to Len, and I wanted this to be a "real world" test. Nobody takes LenB at face value (right?). And it was actually faster than just returning LenB!
Results
I started with 1,000,000 iterations because there was little time difference before that (which itself says a lot). The time difference is minimal:
Even at 10,000,000 iterations we see little separation.
Increasing to 100 million iterations yields little difference:
Only at 1 billion iterations do we start to see the difference, and even then I don't think at this point you could even tell which was slower.
Conclusion
I'm going to stick with Len for now. Even though there's hardly a difference under normal loop conditions, it is slightly slower, and I'm too used to using Len to switch (and have to remember to divide by 2 to get the "true" length).
Hi JP,
I love this series of articles on which code is faster because some people insist that you must use a particular function because it is so much faster. A few years ago I have no doubt that their point of view would be correct. But I believe that the vastly increased speed of modern computers has greatly narrowed the differences in speed to where the point is usually moot. It is great to have documentation about these things. Thanks!
I think the main use for lenb is when you are testing to see if it is zero length or not, which, in my applications, at least, I do quite a bit. I don't think using LenB to actually get the length of a string is useful (especially if it is a type of string that holds more than two bytes per character). So a better test, where you would more likely use LenB would be like the following:
For i = 0 to 1000000 If LenB(s1) then 'Nothing end if next iVersus
For i = 0 to 1000000 If Len(s1) then 'Nothing end if next iFrom my tests, I've found LenB to be faster by a significant amount, especially if you need to grab a ton of data and need to make sure each piece of data isn't a null string.
Just trying to defend the use of LenB