Yo, you guys are right, that was a clearcut case of premature opimization, the root of all evil
This (adopted from someone's post to memory buffer):
Code: Select all
Procedure HexToBuf(buf.s, *buff)
Protected i.i, lenbuf.i
lenbuf = Len(buf)
If lenbuf = 0: ProcedureReturn : EndIf
For i = 1 To lenbuf Step 2
PokeA(*buff+((i-1)/2), Val("$" + Mid(buf,i,2)))
Next i
EndProcedure
Takes around 14700 ms on my machine, whereas my method takes around 15600 ms.
By the way, of course I didn't enter the hash table by hand, I used a loop like GPI did to generate the code for copy&paste. I often generate code like that, but this time it was really unnecessary. My guess is that hardcoded nested select statements could be fastest, but I might be wrong.
Benchmark (my monster map not included again in order not to clutter the forum):
Code: Select all
Procedure HexToBuf(buf.s, *buff)
Protected i.i, lenbuf.i
lenbuf = Len(buf)
If lenbuf = 0: ProcedureReturn : EndIf
For i = 1 To lenbuf Step 2
PokeA(*buff+((i-1)/2), Val("$" + Mid(buf,i,2)))
Next i
EndProcedure
Define *buff = AllocateMemory(1024)
Define.s s
Define.i mt1, mt2
RandomData(*buff, 1024)
s = ConvertBufferToHexString(*buff)
mt1=ElapsedMilliseconds()
For i = 1 To 20000
ConvertHexStringToBuffer(s,*buff)
Next
mt2=ElapsedMilliseconds()
Debug "Method one: "+Str(mt2-mt1)+" ms"
mt1=ElapsedMilliseconds()
For i = 1 To 20000
HexToBuf(s, *buff)
Next
mt2=ElapsedMilliseconds()
Debug "Method two: "+Str(mt2-mt1)+ " ms"