Faster Len(String$) with asm
Posted: Sat Oct 14, 2006 6:06 am
And here a example for Len(String$) with asm (need min. pentium 4):
Greeting
Helle
Code: Select all
;- "Helle" Klaus Helbing, 14.10.2006, PB4.00
Global X.l
Global Len.l
Global String$ = "pokjhgskioUIOHR%(!ยง)AGJOjiioe328959askfakgj9e6t306rgrjhg409604tkmgoe"
;-------- Test with SSE
TestTime.l = ElapsedMilliseconds()
X = @String$ ;string-pointer
For i = 1 To 10000000
!mov ebx,[v_X]
!pxor xmm1,xmm1 ;xmm1 null
W1:
!movdqu xmm0,[ebx] ;read 16 byte of string
!add ebx,16
!pcmpeqb xmm0,xmm1 ;compare the 16 bytes of xmm0 with null (xmm1), set byte = 255 if equal (= null) and byte = 0 if not equal
!pmovmskb eax,xmm0 ;copy signum-bits to eax (ax)
!or eax,eax ;null ?
!jz l_w1 ;yes, not found the null-byte of string (end of string)
!sub ebx,17
!test al,255
!jnz l_w2
!xchg al,ah
!add ebx,8
W2:
!inc ebx
!shr ax,1
!jnc l_w2
Next
!sub ebx,[v_X]
!mov [v_Len],ebx
Time = ElapsedMilliseconds() - TestTime
MessageRequester("Len(String) with SSE","Stringlength = " + Str(Len) + Chr(13) + "TestTime = " + Str(Time) + " ms")
;-------- Test with PB
TestTime.l = ElapsedMilliseconds()
For i = 1 To 10000000
Len = Len(String$)
Next
Time = ElapsedMilliseconds() - TestTime
MessageRequester("Len(String) with PB","Stringlength = " + Str(Len) + Chr(13) + "TestTime = " + Str(Time) + " ms")
End
Helle