loopLength = 10000
a$ = Space(loopLength)
t = ElapsedMilliseconds()
For i = 1 To CountString(a$, " ")
a + 1
Next
Debug ElapsedMilliseconds() - t
t = ElapsedMilliseconds()
For i = 1 To loopLength
a + 1
Next
Debug ElapsedMilliseconds() - t
It's evidently faster.
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too!Please visit my YouTube Channel
loopLength = 50000
a$ = Space(loopLength)
t = ElapsedMilliseconds()
For i = 1 To Len(a$)
a + 1
Next
Debug ElapsedMilliseconds() - t ; 1655 ms
t = ElapsedMilliseconds()
For i = 1 To loopLength
a + 1
Next
Debug ElapsedMilliseconds() - t ; 3 ms!
So it seems you should use a variable after "To" in all "For" loops, for the fastest performance. I'm truly shocked!
DisableDebugger
Procedure.s P1(s.s)
ProcedureReturn s
EndProcedure
Procedure.s P2(s.s)
ProcedureReturn PeekS(@s)
EndProcedure
s.s = Space(10000)
t1 = ElapsedMilliseconds()
For i = 1 To 10000
P1(s)
Next
t2 = ElapsedMilliseconds()
For i = 1 To 10000
P2(s)
Next
t3 = ElapsedMilliseconds()
MessageRequester("Results", Str(t2-t1)+" vs "+Str(t3-t2))
Another thing is that when needing a temporary buffer, it can be faster to use a local Array (which is automatically freed) compared to AllocateMemory/FreeMemory.
DisableDebugger
Procedure.s P1()
Protected result.s, *mem
*mem = AllocateMemory(10000)
FillMemory(*mem, 10000, 32)
result = PeekS(*mem)
FreeMemory(*mem)
ProcedureReturn result
EndProcedure
Procedure.s P2()
Protected Dim buffer.a(9999)
FillMemory(@buffer(), 10000, 32)
ProcedureReturn PeekS(@buffer())
EndProcedure
t1 = ElapsedMilliseconds()
For i = 1 To 10000
P1()
Next
t2 = ElapsedMilliseconds()
For i = 1 To 10000
P2()
Next
t3 = ElapsedMilliseconds()
MessageRequester("Results", Str(t2-t1)+" vs "+Str(t3-t2))
Functions, regardless of where they are called, would always be executed. In the case of loops, variants (eg. function results) must be repeatedly evaluated as the compiler cannot know if they have changed during the iterations of the loop. Some smart compilers would actually optimise this by substituting the function with its static result, after determining that the contents of the loop do not alter the results of the variant.
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too!Please visit my YouTube Channel
DisableDebugger
Procedure.s P1()
NewList MyList()
For i=1 To 10
AddElement (MyList())
Next
EndProcedure
Procedure.s P2()
Static NewList MyList()
ClearList (MyList())
For i=1 To 10
AddElement (MyList())
Next
EndProcedure
t1 = ElapsedMilliseconds()
For i = 1 To 10000
P1()
Next
t2 = ElapsedMilliseconds()
For i = 1 To 10000
P2()
Next
t3 = ElapsedMilliseconds()
MessageRequester("Results", Str(t2-t1)+" vs "+Str(t3-t2))
Nice ideas, so wilberts demo was quite interesting, even I would do move the '@' from the Procedure P2 to the calling instance P2(@s)...
I am also wondering if lists are speedier all the time - on my computer the traditional array can be much faster (but when playing around with #SpeedUp you might see also other results):
DisableDebugger
Procedure.s P1()
#SpeedUp=1
Static Dim MyArr(0)
For i=1 To 10
CompilerIf #SpeedUp
If ArraySize(MyArr())<i
ReDim MyArr(((i>>#SpeedUp)+1)<<#SpeedUp)
EndIf
CompilerElse
ReDim MyArr(i)
CompilerEndIf
MyArr(i)=i
Next
EndProcedure
Procedure.s P2()
Static NewList MyList()
ClearList (MyList())
For i=1 To 10
AddElement (MyList())
Next
EndProcedure
#Loop=100000
t1-ElapsedMilliseconds()
For i = 1 To #Loop
P1()
Next
t2-ElapsedMilliseconds()
For i = 1 To #Loop
P2()
Next
t3-ElapsedMilliseconds()
MessageRequester("Results", Str(t1-t2)+" vs "+Str(t2-t3))