Hi guys,
I'm attempting to optimize my game, and as I was doing so a fairly obvious question occurred to me... Which technologies are faster in App Game Kit? Do different devices affect this as well?
So I've written a little program (if you see a way of improving it please let me know!) that tests the following technologies.
Case Statements
If Statements
For next loop
Until loop
EndWhile loop
Do loop
I've further divided this down further.
Decision making with binary options
Decision making with dual deca (20) options
Case statements are straight forward.
If statements can be used in several different ways so i've broken this down into further sections.
If Else Endif
If EndIf
If Elseif Endif
With the binary option, we can do a simple if, else, endif. Obviously this won't work for 20 options but it will compare the If statement technology to the Case statement technology in a "one or the other" situation. The simple else is abandoned for the 20 options method.
The loops are straight forward, they have a fixed value to reach and then they terminate. This is for direct comparison with FOR and NEXT. There are clearly going to be situations where Until and While can be triggered earlier in a loop meaning less cycles expended. But having said that in a head to head, I wondered which is faster.
// Project: Case vs IF
// Created: 2015-09-07
// set window properties
SetWindowTitle( "Case vs IF" )
SetWindowSize( 1024, 768, 0 )
SetSyncRate( 0, 1 )
// set display properties
SetVirtualResolution( 1024, 768 )
SetOrientationAllowed( 1, 1, 1, 1 )
Print( "Working" )
Sync(): Sync()
cycles = 2147483647 / 100 * 2
tmp# = timer()
For i = 0 to cycles
rnd = random( 0, 1 )
Select rnd
Case 0
`do nothing
EndCase
Case 1
`do nothing
EndCase
EndSelect
Next i
bincasetime# = timer() - tmp#
Print( "Case Statement Time (binaray): " + str( bincasetime# ) )
Sync()
tmp# = timer()
For i = 0 to cycles
rnd = random( 0, 1 )
If rnd = 0
`do nothing
EndIf
If rnd = 1
`do nothing
EndIf
Next i
binendiftime# = timer() - tmp#
Print( "Case Statement Time (binaray): " + str( bincasetime# ) )
Print( "If EndIf Time (binaray): " + str( binendiftime# ) )
Sync()
tmp# = timer()
For i = 0 to cycles
rnd = random( 0, 1 )
If rnd = 0
`do nothing
Else
`do nothing
EndIf
Next i
binelseendiftime# = timer() - tmp#
Print( "Case Statement Time (binaray): " + str( bincasetime# ) )
Print( "If EndIf Time (binaray): " + str( binendiftime# ) )
Print( "If Else EndIf Time (binaray): " + str( binelseendiftime# ) )
Sync()
tmp# = timer()
For i = 0 to cycles
rnd = random( 0, 1 )
If rnd = 0
`do nothing
ElseIf rnd = 1
`do nothing
EndIf
Next i
binelseiftime# = timer() - tmp#
Print( "Case Statement Time (binaray): " + str( bincasetime# ) )
Print( "If EndIf Time (binaray): " + str( binendiftime# ) )
Print( "If Else EndIf Time (binaray): " + str( binelseendiftime# ) )
Print( "If ElseIf EndIf Time (binaray): " + str( binelseiftime# ) )
Sync()
tmp# = timer()
For i = 0 to cycles
rnd = random( 0, 20 )
Select rnd
Case 0
`do nothing
EndCase
Case 1
`do nothing
EndCase
Case 2
`do nothing
EndCase
Case 3
`do nothing
EndCase
Case 4
`do nothing
EndCase
Case 5
`do nothing
EndCase
Case 6
`do nothing
EndCase
Case 7
`do nothing
EndCase
Case 8
`do nothing
EndCase
Case 9
`do nothing
EndCase
Case 10
`do nothing
EndCase
Case 11
`do nothing
EndCase
Case 12
`do nothing
EndCase
Case 13
`do nothing
EndCase
Case 14
`do nothing
EndCase
Case 15
`do nothing
EndCase
Case 16
`do nothing
EndCase
Case 17
`do nothing
EndCase
Case 18
`do nothing
EndCase
Case 19
`do nothing
EndCase
Case 20
`do nothing
EndCase
EndSelect
Next i
casetime# = timer() - tmp#
Print( "Case Statement Time (binaray): " + str( bincasetime# ) )
Print( "If EndIf Time (binaray): " + str( binendiftime# ) )
Print( "If Else EndIf Time (binaray): " + str( binelseendiftime# ) )
Print( "If ElseIf EndIf Time (binaray): " + str( binelseiftime# ) )
Print( "Case Statement Time: " + str( casetime# ) )
Sync()
/*
tmp# = timer()
For i = 0 to cycles
rnd = random( 0, 20 )
If rnd = 0 then `do nothing
If rnd = 1 then `do nothing
If rnd = 2 then `do nothing
If rnd = 3 then `do nothing
If rnd = 4 then `do nothing
If rnd = 5 then `do nothing
If rnd = 6 then `do nothing
If rnd = 7 then `do nothing
If rnd = 8 then `do nothing
If rnd = 9 then `do nothing
If rnd = 10 then `do nothing
If rnd = 11 then `do nothing
If rnd = 12 then `do nothing
If rnd = 13 then `do nothing
If rnd = 14 then `do nothing
If rnd = 15 then `do nothing
If rnd = 16 then `do nothing
If rnd = 17 then `do nothing
If rnd = 18 then `do nothing
If rnd = 19 then `do nothing
If rnd = 20 then `do nothing
Next i
ifthentime# = timer() - tmp#
*/
tmp# = timer()
For i = 0 to cycles
rnd = random( 0, 20 )
If rnd = 0
`do nothing
EndIF
If rnd = 1
`do nothing
EndIF
If rnd = 2
`do nothing
EndIF
If rnd = 3
`do nothing
EndIF
If rnd = 4
`do nothing
EndIF
If rnd = 5
`do nothing
EndIF
If rnd = 6
`do nothing
EndIF
If rnd = 7
`do nothing
EndIF
If rnd = 8
`do nothing
EndIF
If rnd = 9
`do nothing
EndIF
If rnd = 10
`do nothing
EndIF
If rnd = 11
`do nothing
EndIF
If rnd = 12
`do nothing
EndIF
If rnd = 13
`do nothing
EndIF
If rnd = 14
`do nothing
EndIF
If rnd = 15
`do nothing
EndIF
If rnd = 16
`do nothing
EndIF
If rnd = 17
`do nothing
EndIF
If rnd = 18
`do nothing
EndIF
If rnd = 19
`do nothing
EndIF
If rnd = 20
`do nothing
EndIF
Next i
ifendiftime# = timer() - tmp#
Print( "Case Statement Time (binaray): " + str( bincasetime# ) )
Print( "If EndIf Time (binaray): " + str( binendiftime# ) )
Print( "If Else EndIf Time (binaray): " + str( binelseendiftime# ) )
Print( "If ElseIf EndIf Time (binaray): " + str( binelseiftime# ) )
Print( "Case Statement Time: " + str( casetime# ) )
Print( "If EndIF Time: " + str( ifendiftime# ) )
Sync()
tmp# = timer()
For i = 0 to cycles
rnd = random( 0, 20 )
if rnd = 0
`do nothing
Elseif rnd = 1
`do nothing
Elseif rnd = 2
`do nothing
Elseif rnd = 3
`do nothing
Elseif rnd = 4
`do nothing
Elseif rnd = 5
`do nothing
Elseif rnd = 6
`do nothing
Elseif rnd = 7
`do nothing
Elseif rnd = 8
`do nothing
Elseif rnd = 9
`do nothing
Elseif rnd = 10
`do nothing
Elseif rnd = 11
`do nothing
Elseif rnd = 12
`do nothing
Elseif rnd = 13
`do nothing
Elseif rnd = 14
`do nothing
Elseif rnd = 15
`do nothing
Elseif rnd = 16
`do nothing
Elseif rnd = 17
`do nothing
Elseif rnd = 18
`do nothing
Elseif rnd = 19
`do nothing
Elseif rnd = 20
`do nothing
EndIf
Next i
ifelseiftime# = timer() - tmp#
Print( "Case Statement Time (binaray): " + str( bincasetime# ) )
Print( "If EndIf Time (binaray): " + str( binendiftime# ) )
Print( "If Else EndIf Time (binaray): " + str( binelseendiftime# ) )
Print( "If ElseIf EndIf Time (binaray): " + str( binelseiftime# ) )
Print( "Case Statement Time: " + str( casetime# ) )
Print( "If EndIF Time: " + str( ifendiftime# ) )
Print( "If ElseIF Time: " + str( ifelseiftime# ) )
Sync()
tmp# = timer()
For i = 0 to cycles
`do nothing
Next i
nexttime# = timer() - tmp#
Print( "Case Statement Time (binaray): " + str( bincasetime# ) )
Print( "If EndIf Time (binaray): " + str( binendiftime# ) )
Print( "If Else EndIf Time (binaray): " + str( binelseendiftime# ) )
Print( "If ElseIf EndIf Time (binaray): " + str( binelseiftime# ) )
Print( "Case Statement Time: " + str( casetime# ) )
Print( "If EndIF Time: " + str( ifendiftime# ) )
Print( "If ElseIF Time: " + str( ifelseiftime# ) )
Print( "For loop Time: " + str( nexttime# ) )
Sync()
tmp# = timer()
i = -1
Repeat
inc i
`do nothing
Until i = cycles
Untiltime# = timer() - tmp#
Print( "Case Statement Time (binaray): " + str( bincasetime# ) )
Print( "If EndIf Time (binaray): " + str( binendiftime# ) )
Print( "If Else EndIf Time (binaray): " + str( binelseendiftime# ) )
Print( "If ElseIf EndIf Time (binaray): " + str( binelseiftime# ) )
Print( "Case Statement Time: " + str( casetime# ) )
Print( "If EndIF Time: " + str( ifendiftime# ) )
Print( "If ElseIF Time: " + str( ifelseiftime# ) )
Print( "For loop Time: " + str( nexttime# ) )
Print( "Until loop Time: " + str( Untiltime# ) )
Sync()
tmp# = timer()
i = -1
While i < cycles
inc i
`do nothing
EndWhile
whiletime# = timer() - tmp#
Print( "Case Statement Time (binaray): " + str( bincasetime# ) )
Print( "If EndIf Time (binaray): " + str( binendiftime# ) )
Print( "If Else EndIf Time (binaray): " + str( binelseendiftime# ) )
Print( "If ElseIf EndIf Time (binaray): " + str( binelseiftime# ) )
Print( "Case Statement Time: " + str( casetime# ) )
Print( "If EndIF Time: " + str( ifendiftime# ) )
Print( "If ElseIF Time: " + str( ifelseiftime# ) )
Print( "For loop Time: " + str( nexttime# ) )
Print( "Until loop Time: " + str( Untiltime# ) )
Print( "While loop: " + str( whiletime# ) )
Sync()
i = -1
tmp# = timer()
Do
Inc i
`do nothing
If i = cycles then exit
Loop
dotime# = timer() - tmp#
benchmark# = bincasetime# + binendiftime# + binelseendiftime# + binelseiftime# + casetime# + ifendiftime# + ifelseiftime# + nexttime# + Untiltime# + whiletime# + dotime#
do
Print( "Case Statement Time (binaray): " + str( bincasetime# ) )
Print( "If EndIf Time (binaray): " + str( binendiftime# ) )
Print( "If Else EndIf Time (binaray): " + str( binelseendiftime# ) )
Print( "If ElseIf Time (binaray): " + str( binelseiftime# ) )
Print( "Case Statement Time: " + str( casetime# ) )
Print( "If EndIF Time: " + str( ifendiftime# ) )
Print( "If ElseIF Time: " + str( ifelseiftime# ) )
Print( "For loop Time: " + str( nexttime# ) )
Print( "Until loop Time: " + str( Untiltime# ) )
Print( "While loop: " + str( whiletime# ) )
Print( "Do loop: " + str( dotime# ) )
Print( "" )
Print( "Device benchmark " + str( benchmark#, 2 ) )
Print( "End of tests" )
Sync()
loop
I also tallyed up all the times which equates to a benchmark.
Basically the lower the value the faster the machine.