Quicker choose function...
Posted: Fri Dec 23, 2016 6:00 pm
I often need a function which returns a specific value if a condition is true and another value if not.
For example, I draw a box in a different color if the mouse will be moved over the box, this will be handled with something like DrawBox(index, Choose(Bool(index=mouse), ColorHighlight, ColorDefault)).
But the Choose function is very slow...
So I use now this one which is nearly 100 times quicker...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Here's the speed test...
For example, I draw a box in a different color if the mouse will be moved over the box, this will be handled with something like DrawBox(index, Choose(Bool(index=mouse), ColorHighlight, ColorDefault)).
But the Choose function is very slow...
Code: Select all
Procedure Choose(a,b,c)
If a
ProcedureReturn b
Else
ProcedureReturn c
EndIf
EndProcedure
Code: Select all
Macro ist(a,b,c)
(Bool(a)*(b)+Bool(a)!1*(c))
EndMacro
Here's the speed test...
Code: Select all
CompilerSet Debugger=0, Assembler=1
Procedure Choose1(a,b,c)
If a
ProcedureReturn b
Else
ProcedureReturn c
EndIf
EndProcedure
Macro Choose2(a,b,c)
(Bool(a)*b+Bool(a)!1*c)
EndMacro
#Loop=9999999
#Add1=123
#Add2=1000000000-#Add1*#Loop
t1-ElapsedMilliseconds()
For i=0 To #Loop
z1+Choose1(Bool(i<>5),#Add1,#Add2)
Next i
t1+ElapsedMilliseconds()
t2-ElapsedMilliseconds()
For i=0 To #Loop
z2+Choose2(Bool(i<>5),#Add1,#Add2)
Next i
t2+ElapsedMilliseconds()
MessageRequester("Test using "+Str(#Add1)+" / "+Str(#Add2),Str(z1)+", "+Str(t1)+" ms"+#CR$+Str(z2)+", "+Str(t2)+" ms")