Code: Alles auswählen
; getestet mit PB 4.31 unter Windows XP
EnableExplicit
Procedure Translate1 (*text, textLen, Array trans.b(1))
Protected *textPtr.Byte
; iterate through memory and replace bytes
For *textPtr = *text To *text + textLen - 1
*textPtr\b = trans(*textPtr\b)
Next
EndProcedure
Procedure Translate2 (*text, textLen, *trans)
; iterate through memory and replace bytes
! mov edx, [p.p_text]
! mov ecx, [p.v_textLen]
! mov eax, [p.p_trans]
! push ebx
! mov ebx, eax
! TransLoop:
! mov al, [edx+ecx]
! xlatb
! mov [edx+ecx], al
! loop TransLoop
! pop ebx
EndProcedure
;-- Demo
Define n, i, t
Define old$, new$, s$
n = 500000 ; Anzahl der Schleifen (evtl. je nach PC verkleinern oder vergrößern)
old$ = "123456789"
new$ = "ABCDEFGHI"
; build translation table
Dim trans.b(255)
For i = 0 To 255
trans(i) = i
Next
For i = 1 To Len(old$)
trans(Asc(Mid(old$,i,1))) = Asc(Mid(new$,i,1))
Next
OpenConsole()
; translate string (1)
s$ = "Record 1; Record 2; Record 3; Record 4; Record 5; Record 6; Record 7; Record 8; Record 9"
t = ElapsedMilliseconds()
For i = 1 To n
Translate1(@s$, Len(s$), trans())
Next
t = ElapsedMilliseconds() - t
PrintN("'" + s$ + "'" + #LF$ + Str(t) + " ms")
; translate string (2)
s$ = "Record 1; Record 2; Record 3; Record 4; Record 5; Record 6; Record 7; Record 8; Record 9"
t = ElapsedMilliseconds()
For i = 1 To n
Translate2(@s$, Len(s$), @trans(0))
Next
t = ElapsedMilliseconds() - t
PrintN("'" + s$ + "'" + #LF$ + Str(t) + " ms")
Input()
Translate1() 406 ms
Translate2() 266 ms
Also ist Translate2() wohl doch schneller. Ich weiß aber nicht, wie weit die Ergebnisse eines Tests mit 1/2 Million Wiederholungen relevant für die Praxis sind.
Gruß, Little John