Benefits of inline if?

General FreeBASIC programming questions.
Imortis
Moderator
Posts: 1924
Joined: Jun 02, 2005 15:10
Location: USA
Contact:

Benefits of inline if?

Post by Imortis »

While working on the RTlib, I see an awful lot of inline if code. I know C and FB have them, but I never really used them myself. Is there a benefit to using them other than shorter source code? Is it faster in execution? Or in compilation?
bcohio2001
Posts: 556
Joined: Mar 10, 2007 15:44
Location: Ohio, USA
Contact:

Re: Benefits of inline if?

Post by bcohio2001 »

IMHO
It's more of ascetics. I beleave that after compile it all boils down to same asm.

Back in the day when disk space was limited, it was less disk space to be inline.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Benefits of inline if?

Post by MrSwiss »

I'd say (having run many speed tests), it's probably:
Imortis wrote:Is it faster in execution?
If checking a x/y/w/h range (inside/outside e.g. Mouse-Pos.), it is faster to do:

Code: Select all

If x > (something) AndAlso y > (someting) Then
	If w > (something) AndAlso h > (someting) Then
		...	' execute conditional
	End If
End If
than writing it all in one line (with two more AndAlso statements, less one If).

Binary is slower than Boolean eval. (also: not a 'short-cut' Operator) e.g. And ...
St_W
Posts: 1626
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: Benefits of inline if?

Post by St_W »

@MrSwiss: i guess Imortis was referring to inline if (IIF).

IMHO it's just a convenient shortcut, which allows to save lines and temporary variables. I don't think that is has any influence on the execution speed (at least not with an optimizing compiler).
Munair
Posts: 1286
Joined: Oct 19, 2017 15:00
Location: Netherlands
Contact:

Re: Benefits of inline if?

Post by Munair »

We shouldn't guess.

IIF is not Inline If. Rather it means Immediate If.

Maybe the OP can elaborate on his question.
Imortis
Moderator
Posts: 1924
Joined: Jun 02, 2005 15:10
Location: USA
Contact:

Re: Benefits of inline if?

Post by Imortis »

I was referring to IIF, or specifically the C equivalent:

Code: Select all

( test ? true : false )
I always heard it as "Inline If" because the whole structure was IN one LINE. That is how they explained it in my C class in college.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Benefits of inline if?

Post by MrSwiss »

Imortis wrote:... the whole structure was IN one LINE
This is definitely a very ambiguous explanation ...

Inline/inlining refers to code (not: In One Line) as done with:
  • #Define and/or #Macro
    compiler optimisations:
    • inlining of procedures (Sub/Function)
      ... (and probably other optimisations)
IIF saves IMHO, only on temporary Boolean's ... (don't know, if it has any speed impacts).
Imortis
Moderator
Posts: 1924
Joined: Jun 02, 2005 15:10
Location: USA
Contact:

Re: Benefits of inline if?

Post by Imortis »

MrSwiss wrote:This is definitely a very ambiguous explanation ...
Sorry? It was never specifically named in my textbook and that was what the instructor called it. My mistake.
St_W
Posts: 1626
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: Benefits of inline if?

Post by St_W »

Munair wrote:IIF is not Inline If. Rather it means Immediate If.
No, "inline if" is the correct term for it. See also Wikipedia: https://en.wikipedia.org/wiki/%3F%3A
Wikipedia wrote:In computer programming, ?: is a ternary operator that is part of the syntax for basic conditional expressions in several programming languages. It is commonly referred to as the conditional operator, inline if (iif), or ternary if.
Note that the IF command cannot be used inline, that means that it can only occur at the beginning of a statement (because it is a statement) and not within an expression (e.g. "1 + 2 - iif(condition, a, b) + 4").
Munair
Posts: 1286
Joined: Oct 19, 2017 15:00
Location: Netherlands
Contact:

Re: Benefits of inline if?

Post by Munair »

Indeed, see wikipedia. It says Immediate: https://en.wikipedia.org/wiki/IIf

I always learned Immediate as the official meaning and never gave it much thought. I think it is the general term used in VisualBasic. Looking up some internet articles, it appears both Immediate and Inline are used and the terms seem to be interchangable.

Immediate seems more logical (could be me) because an equivalent IF condition can be written on a single line too.
Last edited by Munair on Jan 11, 2018 22:16, edited 6 times in total.
Linuxbob
Posts: 60
Joined: Sep 01, 2010 1:03
Location: Ohio, USA

Re: Benefits of inline if?

Post by Linuxbob »

I think it's a style thing more than anything else.

For simple tests and assignments, the classic IF form is more familiar to me, probably because I came up in the 70s and 80s.

IF x = 3 then a = 4 else a = 5

But IIF accomplishes the same thing in a more modern syntax.

a = IIF(x = 3, 4, 5)

The IIF syntax looks more elegant to me, but I don't have strong feelings for or against either form.

In either case, I suspect the performance is equivalent. (maybe there is a significant detectable difference in performance if one was trying to do thousands of these comparisons in a program.)
Imortis
Moderator
Posts: 1924
Joined: Jun 02, 2005 15:10
Location: USA
Contact:

Re: Benefits of inline if?

Post by Imortis »

I ask because I saw an awful lot of this (converted to FB):

Code: Select all

#define FB_FILE_TO_HANDLE( index ) _
	(iif(index = 0,_
		(cast(FB_FILE ptr, @FB_HANDLE_SCREEN)),_
		iif( (index) = -1,_
			cast(FB_FILE ptr, @FB_HANDLE_PRINTER),_
			iif( FB_FILE_INDEX_VALID( (index) ),_ 
				FB_FILE_TO_HANDLE_VALID( (index) ),_
				(cast(FB_FILE ptr,(NULL)))_
				)_
			)_
		)_
	)
Not gonna lie: This made me want to run for the hills screaming. I had an error in my translation at first. And all the Error message told me was that I has an error in the like calling the define. I had to unpack this thing into the context it was being called in as a regular IF statement to see where my flaw was.

*FULL BODY SHIVER* What monster wrote this code and said, "I'm okay with this".

EDIT: In case it is not clear, I am exaggerating for comedic effect. It was a bear to deal with, but I am not traumatized by anyone's code.
St_W
Posts: 1626
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: Benefits of inline if?

Post by St_W »

I do not know how or where in the rtlib that macro is used, but the inline nature of iif allows a much wider and more flexible use of it (compared to an equivalent implementation using if statements). Actually a traditional if couldn't probably be used because it cannot return a "result"-value.

That's a good example for the expression vs statement nature of the different ifs ;-)
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Benefits of inline if?

Post by jj2007 »

The ternary operator syntax is not really BASIC, more C perhaps, but one can get used to it:

Code: Select all

  For_ ct=122 To 124
	Print Str$("\nis %i above 123 or not: ", ct), If?(ct ab 123, "above", "not above")
  Next
Output:
is 122 above 123 or not: not above
is 123 above 123 or not: not above
is 124 above 123 or not: above
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: Benefits of inline if?

Post by Tourist Trap »

Hi

as far as I can remember from some tests I made previously, IIF are necessary with DEFINEs... Otherwise the text replacement will fail in some expressions. IIF can sit in a single line instruction, while IF EnDIF require generally line breaks that split the whole thing.

I'm not a pro so sorry if I'm not clear. Anyway it should be clear enough, unless I'm really tired tonight (quite a possible thing!).
Post Reply