I've been intending to re-write some of my earlier PureBasic code that uses PokeS(), by instead using a structure pointer (test 3), but I'm finding that my original use of PokeS() is already faster. This is largely because I work with fixed blocks of data, but there are cases where I would still prefer to re-write the code. One reason is because byte-by-byte enables me to act on certain intelligent logic.
Test 3 below is already quite quick if I turn off the debugger and output the results to the console. I get around 33ms for 2,000,000 bytes. So not too bad I suppose.
Code: Select all
EnableExplicit
Define t1, t2, t3
Define cpstring.s = Space(1000000)
Define lendata.i = Len(cpstring.s) * 2
Define *src ; Start address of source data
Define *srcptr.byte ; Source data pointer
Define *dest ; Start address of destination
Define *destptr.byte ; Destination data pointer
Define *limit
*dest = AllocateMemory(lendata.i)
; **
; ** Test 1, PokeS()
; **
t1 = ElapsedMilliseconds()
PokeS(*dest, cpstring.s, lendata.i, #PB_String_NoZero)
Debug "Time 1 = " + Str(ElapsedMilliseconds() - t1)
; **
; ** Test 2, CopyMemory()
; **
t2 = ElapsedMilliseconds()
CopyMemory(@cpstring.s, *dest, lendata.i)
Debug "Time 2 = " + Str(ElapsedMilliseconds() - t2)
; **
; ** Test 3, Byte copy
; **
t3 = ElapsedMilliseconds()
*src = @cpstring.s ; Start address of source data
*srcptr = *src ; Set starting pointer
*destptr = *dest ; Set destination pointer
*limit = *dest + lendata.i
While *destptr < *limit
*destptr\b = *srcptr\b ; Copy the byte
*srcptr + 1 ; Advance source pointer
*destptr + 1 ; Advance destination pointer
Wend
Debug "Time 3 = " + Str(ElapsedMilliseconds() - t3)



