Bool vs. If
Posted: Thu Jul 28, 2022 3:24 am
Hi #PB_All,
I experimented with the question whether If or Bool is faster in the following example:
On my computer (Windows 10) Bool is a bit faster (1549ms vs. 1712ms). Is this always the case, on all platforms?
Also, the following code has an even wider gap between the results:
...which is somehow interesting, because I thought the *-operation would take more time than just storing a value.
I experimented with the question whether If or Bool is faster in the following example:
Code: Select all
DisableDebugger
Define a=10,b,c,tick1.q,tick2.q,tick3.q
tick1=ElapsedMilliseconds()
For b=1 To 1000000000
c=Bool(a=10)
Next
tick2=ElapsedMilliseconds()
For b=1 To 1000000000
If a=10
c=1
EndIf
Next
tick3=ElapsedMilliseconds()
EnableDebugger
Debug "Bool: "+Str(tick2-tick1)
Debug "If: "+Str(tick3-tick2)
Also, the following code has an even wider gap between the results:
Code: Select all
DisableDebugger
Define a=10,b,c,tick1.q,tick2.q,tick3.q
tick1=ElapsedMilliseconds()
For b=1 To 1000000000
c=5*Bool(a=10)
Next
tick2=ElapsedMilliseconds()
For b=1 To 1000000000
If a=10
c=50
EndIf
Next
tick3=ElapsedMilliseconds()
EnableDebugger
Debug "Bool: "+Str(tick2-tick1)
Debug "If: "+Str(tick3-tick2)