Code: Select all
testtable = {}
For n=1 To 100
InsertItem(testtable, {name="item"..n, number=Rnd(1)} )
Next
StartTimer(1)
amount = TableItems(testtable)
For n=1 To 10000
For m = 0 To amount-1
item = testtable[m]
If item.number = 1 Then item.number=2
If item.number = 0 Then item.number=2
Next
Next
time = GetTimer(1)
DebugPrint(time)
ResetTimer(1)
amount = TableItems(testtable)
For n=1 To 10000
For m = 0 To amount-1
If testtable[m].number = 1 Then testtable[m].number=2
If testtable[m].number = 2 Then testtable[m].number=2
Next
Next
time = GetTimer(1)
DebugPrint(time)
ResetTimer(1)
For n=1 To 10000
ForEach(testtable, Function (ID, item)
If item.number = 1 Then item.number=2
If item.number = 2 Then item.number=2
EndFunction)
Next
time = GetTimer(1)
DebugPrint(time)
ResetTimer(1)
For n=1 To 10000
ForEach(testtable, Function (ID, item)
If item.number = 1
item.number=2
ElseIf item.number = 2
item.number=2
EndIf
EndFunction)
Next
time = GetTimer(1)
DebugPrint(time)
ResetTimer(1)
For n=1 To 10000
ForEach(testtable, Function (ID, item)
If item.number = 1
item.number=2
Else
item.number=2
EndIf
EndFunction)
Next
time = GetTimer(1)
DebugPrint(time)
1360 (for 1 to x - indirect call to a variable inside a table)
1183 (for 1 to x - direct call to a variable inside a table)
1025 (two IFs)
1173 (IF-ELSEIF-ENDIF)
966 (IF-ELSE-ENDIF)
Meaning that FOREACH is clearly faster than FOR n=1 to x
Also it appears IF-ELSE-ENDIF is faster than two IFs or IF-ELSEIF-ENDIF structure.
I was expecting two IFs to be slower than IF-ELSEIF-ENDIF structure, but appears it is faster using two IFs instead.
I might do more speed tests and report about them here.