[bmx] Quick If by Warpy [ 1+ years ago ]

Started by BlitzBot, June 29, 2017, 00:28:42

Previous topic - Next topic

BlitzBot

Title : Quick If
Author : Warpy
Posted : 1+ years ago

Description : In lots of other languages, there is a ternary operator
a ? b : c

which returns b if a is true, and c otherwise.
It's useful when you've got a quick either/or decision to make and don't want to write out a load of syntax for an If statement.

Because numbers aren't proper objects in Blitz, I can't make this function work quite as smoothly as it should, but one could write different versions for numbers and objects.


Code :
Code (blitzmax) Select
Function qif$(p,a$,b$)
If p Return a Else Return b
End Function


For c=1 To 4
Print String(c)+" "+qif(c Mod 2 = 1, "odd","even")
Next


Comments :


GIB3D(Posted 1+ years ago)

 Hey, I made something similar.
Function ReturnIfTrue$(boolean,true_value$,false_value$="")
If boolean
Return true_value
Else
Return false_value
EndIf
End Function



Czar Flavius(Posted 1+ years ago)

 Nice idea, but this is harder to read and will be slower than a "non-quick" if.


Pineapple(Posted 1+ years ago)

 I've got something faster.SuperStrict

Framework brl.StandardIO
Import brl.blitz



Function qif_old$(p%,a$,b$)
If p Return a Else Return b
End Function

Function qif_new$(p%,a$,b$)
If p Return a
Return b
End Function



Local ms%

ms%=MilliSecs()
For Local t%=1 To 3200000
Local x$=qif_old(t Mod 2, "odd","evn")
Next
Print "Old: "+(MilliSecs()-ms)+" ms"

ms%=MilliSecs()
For Local t%=1 To 3200000
Local x$=qif_new(t Mod 2, "odd","evn")
Next
Print "New: "+(MilliSecs()-ms)+" ms"
Run it and observe the difference. (About 32 ms on my machine)EDIT: This is more than twice as fast:C Extern "qif.c"char qif( int condition , char if_true , char if_false )
{
if ( condition ) { return if_true; }
return if_false;
}
Speed test:SuperStrict

Framework brl.StandardIO
Import brl.blitz
Import "qif.c"

Extern
Function qif:String(condition:Int,if_true$,if_false$)
End Extern

Function qif_new$(p%,a$,b$)
If p Return a
Return b
End Function




Local ms%

ms%=MilliSecs()
For Local t%=1 To 3200000
Local x$=qif_new(t Mod 2, "odd","evn")
Next
Print "New:    "+(MilliSecs()-ms)

ms%=MilliSecs()
For Local t%=1 To 3200000
Local x$=qif(t Mod 2, "odd","evn")
Next
Print "Extern: "+(MilliSecs()-ms)



Warpy(Posted 1+ years ago)

 man you're going to be so bummed when you realise that it has to evaluate both return values. This really shouldn't be used if you're worried about execution speed.Good optimisation anyway though!


Streaksy(Posted 1+ years ago)

 Testing each type of IF 530000000 times:   QIF.C External: 2719 ms   Simple QIF Function: 2783 ms   Raw IF Statement: 4756 msBoth types of QIF function here are practically the same speed.  They seem to be faster than the raw IF statement simply because the ELSE is seperated.  I shall find this very useful.  Thanks.  =DThis is the code used:Import "qif.c"
Extern
Function qif:String(condition:Int,if_true$,if_false$)
End Extern


tim=MilliSecs() 'test the qif that uses C
For reps=1 To 530000000
qIf (reps Mod 2),"odd","evn"
Next
Print "qif external: "+(MilliSecs()-tim)

tim=MilliSecs() 'test the qif function that just keeps the ELSE seperate
For reps=1 To 530000000
qif2 (reps Mod 2),"odd","evn"
Next
Print "qif function: "+(MilliSecs()-tim)

tim=MilliSecs() 'test the basic IF THEN ELSE crap
For reps=1 To 530000000
If (reps Mod 2) Then p$="odd" Else p$="evn"
Next
Print "raw if: "+(MilliSecs()-tim)


Function qIf2$(p%,a$,b$)
If p Return a
Return b
End Function



Yasha(Posted 1+ years ago)

 <div class="quote"> Because numbers aren't proper objects in Blitz, I can't make this function work quite as smoothly as it should, but one could write different versions for numbers and objects. </div>Just in case anyone still cares, you could also do this if you wanted a completely parametric (i.e. type-generic but also forces Then and Else to match) version:For c = 1 To 4
Print c + " " + (["even", "odd"][c Mod 2 = 1])
Next
(i.e. construct a two-part array then immediately select from it)Notice that the order of arguments is completely reversed: Else-Then-Test.It's also probably significantly slower since it has to build an array object. [/i]