Code archives/Algorithms/Quick If

This code has been declared by its author to be Public Domain code.

Download source code

Quick If by Warpy2010
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.
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

GIB3D2010
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 Flavius2010
Nice idea, but this is harder to read and will be slower than a "non-quick" if.


Pineapple2010
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)



Warpy2010
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!


Streaksy2010
Testing each type of IF 530000000 times:

QIF.C External: 2719 ms
Simple QIF Function: 2783 ms
Raw IF Statement: 4756 ms

Both 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. =D



This 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



Yasha2014
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.


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.


Code Archives Forum