Shield wrote:
The problem that it is so slow in PB is because of the continuous allocation and deallocation of memory.
I think that problem not in it.
In the second code is also allocated and deallocation memory
Code:
*Str = ReAllocateMemory(*Str, Len)
but the program works fast.
In my opinion, the problem is that every access string, measured its length. And this is done by iterating through all characters and search for zero bytes. It's a slow process.
Need to store the string length (for example, as stored, the size of the array) and not recalculate it every time.
This confirms the experiment.
If the second code to add a function MemoryStringLength(), then it will work just as slowly as the first code.
Code:
DisableDebugger
*Str = AllocateMemory(10)
Len = 10
Pos = 0
#Text = "1234567890"
Time = ElapsedMilliseconds()
For i=1 To 100000
Size = StringByteLength(#Text)
If Pos+Size+4>Len
Len+Size+4
*Str = ReAllocateMemory(*Str, Len)
EndIf
Pos+PokeS(*Str+Pos, #Text, Size)
MemoryStringLength(*Str) ; This is the main reason the slow work With strings!
Next i
EnableDebugger
Result.f = (ElapsedMilliseconds()-Time)/1000
ShowMemoryViewer(*Str, Len)
MessageRequester("", StrF(Result, 3))
Measuring the length of string each time you access the main reason for the slow work of all string functions!