• Welcome to Valhalla Legends Archive.
 

Visual Basic Tip of The Day!

Started by TheMinistered, May 18, 2003, 06:32 PM

Previous topic - Next topic

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!


If (LenB(StringVariable) = 0) Then
   ....
End If


Finally, here is my proof!


'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


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)

Yoni

Use strlen or basic_string::length.

Banana fanna fo fanna

#2
Yeah... What Yoni said.

(Edit: You requested it)