Valhalla Legends Forums Archive | General Programming | Visual Basic Tip of The Day!

AuthorMessageTime
TheMinistered
I have had several people tell me Len is faster than LenB. I have even had Camel run his "test" in which he showed me results that showed Len was faster than LenB. Well Camel, I think it's time to get a new speedometer!

First here is one applicable place to use LenB!

[code]
If (LenB(StringVariable) = 0) Then
....
End If
[/code]

Finally, here is my proof!

[code]
'Len
66024A0A __vbaLenBstr:
66024A0A 8B442404 mov eax,[esp+4]   ; grab the string pointer off the stack
66024A0E 85C0 test eax,eax      ; Is it null?
66024A10 7405 jz loc_66024A17   ; if it is, jump to return
66024A12 8B40FC mov eax,[eax-4]   ; get the length from the BSTR header
66024A15 D1E8 shr eax,1      ; shift right one to convert bytes to chars (2 bytes = 1 char)
66024A17 loc_66024A17:
66024A17 C20400 ret 4

'LenB
660EA517 __vbaLenBstrB:      ; note the shift is missing here
660EA517 8B442404 mov eax,[esp+4]
660EA51B 85C0 test eax,eax
660EA51D 7403 jz loc_660EA522
660EA51F 8B40FC mov eax,[eax-4]
660EA522 loc_660EA522:
660EA522 C20400 ret 4
[/code]

LenB is not a huge savings, it's only one instruction different. However, a savings is a savings!
(Thank's go out to OnError for disassembling this for me while my computer was temporarily down)
May 18, 2003, 11:32 PM
Yoni
Use strlen or basic_string::length.
May 19, 2003, 12:00 AM
St0rm.iD
Yeah... What Yoni said.

(Edit: You requested it)
May 19, 2003, 2:23 AM

Search