# How faster is an 'if' construct compared to an immediate 'if' statement (IIF) and when should I use it?

How does the traditional if statement compare with that of the immediate if statement (IIF, the one usually written as condition ? value_if_true : value_if_false) in terms of speed of execution?

Also, in what scenarios should we be ideally using the IIF?

-
Visual Basic iif? Oxygene iif? Some other language iif? – Yannis Nov 29 '11 at 7:37
What is an "immediate if statement"? – BЈовић Nov 29 '11 at 7:38
@VladimirJovic It's Visual Basic speak for "ternary operator". en.wikipedia.org/wiki/IIf – Yannis Nov 29 '11 at 7:38
@Yannis Rizos@VladimirJovic It is in C,C++ and Java as well not just in VB. And there are other languages that offer this handy structure. – Maxood Nov 29 '11 at 7:51
@Yannis Rizos While we're on the subject of syntax and naming things, ? : is not the ternary operator, it is a ternary operator. While your language might not implement other ternary operators, this is far from the only one possible. Ternary simply means it takes three arguments. If you want to give it a name, call it the conditional operator. – fzwo Nov 29 '11 at 9:24

EDIT: WRONG: Most likely, there is no measurable difference in terms of speed.

CORRECT: Since IIf is a library function in VB, both truepart and falsepart are evaluated, so if one of those is expensive to evaluate but eventually not choosen, it might be much slower than a conventional If.

Therefore, you should be using it when it makes your code more compact and readable.

In many cases, using iif (or the ternary ?: operator in C-like languages) makes it possible to avoid introducing an intermediate variable (or code replication)

Consider:

A = B + C + IIf(D<=3, D, 3)

vs.

If D<=3 Then
E = D
Else
E = 3
End If

A = B + C + E

or

If D<=3 Then
A = B + C + D
Else
A = B + C + 3
End If
-
On IIf efficiency: Because IIf is a library function, it will always require the overhead of a function call, whereas a conditional operator will more likely produce inline code. Not to suggest that the overhead would normally be measurable, but on heavy use it might, depends on the circumstances and what level of optimization is required for the task. – Yannis Nov 29 '11 at 7:50
@ammoQ So its all about simplicity and readability and nothing to do with speed. – Maxood Nov 29 '11 at 7:51
@Yannis Rizos How can you say it is a library function? Do we have to import or include a specific library to use iif? Please explain – Maxood Nov 29 '11 at 7:54
@Maxood I don't say it, the article I link to does. IIf is syntactic sugar, but on compiler/interpreter level it gets translated to something close to a simple if-then-else statement. If you go with the simple if-then-else you won't have the overhead of the translation, but that's minimal at best. "Library function" in this sense is used to differentiate with core language constructs, like operators and statements. You don't have to import it. – Yannis Nov 29 '11 at 8:06
Yannis Rizos: I'm suprised that VB doesn't inline and shortcut it; in that case, the performance implication can be quite heavy, especially when one of the branches does some expensive calculation and the other branch is chosen. Too bad I can't downvote my own answer. – user281377 Nov 29 '11 at 8:16

@ammoQ's answer needs something clarified, he means that the programmer doesn't have to intoduce a temporary variable, not the compiler. In all cases there'll be a temporary variable to hold the result of the conditional operator.

The real difference I know about (at least in C, C++ and Java) is that it's an expression, as opposed to a statement. In other words, it can act as an RValue (I mean a value that can be assigned to a variable). A statement doen't evaluate to a value. This makes their usage more convenient, such as:

int x = (y > 0 ? 1 : 2);

which can't be written as

int x = if (y > 0) 1; else 2;

But rather split into more than one line. This will be noticeable as the logic inside the if/else grows.

-
I've edited your question slightly, you don't need to worry that you posted as an answer, it stands perfectly as an answer. – Yannis Nov 29 '11 at 8:18

As far as performance goes: In a fully-compiled language like C or C++, with optimizations on, the difference is probably zero. In other languages (interpreted, byte-code-compiled, or jit-compiled) there may be a difference, but 1. it's not going to be much and 2. it's not going to be your bottleneck. There are exceptions, but those are rare, and if you encounter one of them, you're probably fiddling with fine-grained profiling and instruction-level optimizations; in all the other cases, either is going to be fast enough.

So, as always, your first choice should be to optimize for readability, not for runtime performance. Which brings us to the question when you should use one or the other; well, opinions differ. One camp says the ternary operator simply shouldn't be used at all, for clarity's sake; and they certainly have a point, because it has the tendency to make for really complicated one-liners, especially if you nest it, but also because its precedence can lead to undesirable results. However, I think there are situations where a ternary operator makes the code more readable, and I don't see anything wrong with using it in such a situation; a textbook example is simple text pluralizing:

printf("%i %s deleted.", num_files, (num_files > 1) ? "files" : "file");

The same code written with an explicit if statement would have taken 5 additional lines without substantially contributing to readability.

That said, IIf in VB has a nasty caveat: unlike If, which is a proper language statement and evaluates only one branch, IIf is a built-in function, so both branches get evaluated, no matter what the condition says. As long as the branches don't have side effects, you won't notice, but when they do, you're in for some serious face-palming.

-

Micro-optimisation is rarely worth worrying about, and whether the ternary operator is executed faster than a full conditional definitely falls under the category of micro-optimisation.

Really, the ternary operator should be used when it makes the code clearer to read and thus easier to maintain and should be avoided where it makes the intent of the code more obscure and thus more difficult to maintain.

If you get to the stage where your application is not performing as well as you need it to perform, then you need to profile your application, benchmarking it with typical and worst case application loads. Only then can you see where you application is running too slowly and thus where to spend your time in optimising.

As it is, you are very unlikely to see any difference between code with uses IIf and code which doesn't, but to be sure you need to benchmark.

Finally, you have to be very careful about using the VB IIf() construct due to issues with side effects. The fact that value_if_true and value_if_false are always evaluated, irrespective of the value of the condition means that the IIf() version could behave very differently to the normal If construct it might be intended to replace.

-