Benefits of inline if?
Benefits of inline if?
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?
-
- Posts: 556
- Joined: Mar 10, 2007 15:44
- Location: Ohio, USA
- Contact:
Re: Benefits of inline if?
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.
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.
Re: Benefits of inline if?
I'd say (having run many speed tests), it's probably: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 ...
If checking a x/y/w/h range (inside/outside e.g. Mouse-Pos.), it is faster to do:Imortis wrote:Is it faster in execution?
Code: Select all
If x > (something) AndAlso y > (someting) Then
If w > (something) AndAlso h > (someting) Then
... ' execute conditional
End If
End If
Binary is slower than Boolean eval. (also: not a 'short-cut' Operator) e.g. And ...
Re: Benefits of inline if?
@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).
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).
Re: Benefits of inline if?
We shouldn't guess.
IIF is not Inline If. Rather it means Immediate If.
Maybe the OP can elaborate on his question.
IIF is not Inline If. Rather it means Immediate If.
Maybe the OP can elaborate on his question.
Re: Benefits of inline if?
I was referring to IIF, or specifically the C equivalent:
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.
Code: Select all
( test ? true : false )
Re: Benefits of inline if?
This is definitely a very ambiguous explanation ...Imortis wrote:... the whole structure was IN one LINE
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)
- inlining of procedures (Sub/Function)
Re: Benefits of inline if?
Sorry? It was never specifically named in my textbook and that was what the instructor called it. My mistake.MrSwiss wrote:This is definitely a very ambiguous explanation ...
Re: Benefits of inline if?
No, "inline if" is the correct term for it. See also Wikipedia: https://en.wikipedia.org/wiki/%3F%3AMunair wrote:IIF is not Inline If. Rather it means Immediate 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").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.
Re: Benefits of inline if?
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.
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.
Re: Benefits of inline if?
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.)
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.)
Re: Benefits of inline if?
I ask because I saw an awful lot of this (converted to FB):
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.
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)))_
)_
)_
)_
)
*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.
Re: Benefits of inline if?
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 ;-)
That's a good example for the expression vs statement nature of the different ifs ;-)
Re: Benefits of inline if?
The ternary operator syntax is not really BASIC, more C perhaps, but one can get used to it:
Output:
is 122 above 123 or not: not above
is 123 above 123 or not: not above
is 124 above 123 or not: above
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
is 122 above 123 or not: not above
is 123 above 123 or not: not above
is 124 above 123 or not: above
-
- Posts: 2958
- Joined: Jun 02, 2015 16:24
Re: Benefits of inline if?
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!).
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!).