The first post example is not really a real-life issue but it happens every now and then to concatenate a lot of strings into one long string. Since 20 years now I always use this small trick to concatenate very fast without any tricks. Works on all platforms:
Code: Select all
; test with no trick
; ===============================================
start.i = ElapsedMilliseconds()
result.s = ""
For x.i = 1 To 30000
result.s + "abcdefghijklmnopqrstuvwxyz";
Next
Debug "Standard concat needed: " + Str(ElapsedMilliseconds() - start.i) + "ms"
; test again with simple hack
; ===============================================
start.i = ElapsedMilliseconds()
result.s = ""
intermediate.s = ""
For x.i = 1 To 30000
intermediate.s + "abcdefghijklmnopqrstuvwxyz";
If Len(intermediate.s) > 4096
result.s + intermediate.s
intermediate.s = ""
EndIf
Next
result.s + intermediate.s ; get the rest intermediate
Debug "Enhanced concat needed: " + Str(ElapsedMilliseconds() - start.i) + "ms"
Result (Intel Core i5 / PB 5.44 / 64 Bit / Debugger On):
Standard concat needed: 11794ms
Enhanced concat needed: 173ms
This works not only with PB but with every dev language. It is only 5 lines more code...
Background:
If you concat a string, the OS behaves like this:
1) Reserve new string memory [oldStringLength]+[stringLengthToAdd]
2) Copy old string memory to new string memory
3) Copy new string to new string memory
4) release old string memory
The longer the string gets, the more time is needed for steps 1 and 2. Thus, working with smaller strings and concatenating the long ones not that often increases the speed!