2

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?

Peter Mortensen
  • 1,050
  • 2
  • 12
  • 14
Maxood
  • 1,503
  • 2
  • 11
  • 19
  • 1
    Visual Basic iif? Oxygene iif? Some other language iif? – yannis Nov 29 '11 at 07:37
  • What is an "immediate if statement"? – BЈовић Nov 29 '11 at 07:38
  • @VladimirJovic It's Visual Basic speak for "ternary operator". http://en.wikipedia.org/wiki/IIf – yannis Nov 29 '11 at 07: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 07:51
  • @Maxood in C and languages that follow its syntax, the IIf alternative is the [ternary operator](http://en.wikipedia.org/wiki/%3F:). There might be some compilers or libraries that offer IIf as it's found in VB and its derivatives, but that's not common. There's a difference, as usually ternary operators are usually core operators of the language, whereas VB IIf is a library function. – yannis Nov 29 '11 at 07:56
  • 4
    @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 09:24
  • @fzwo Well, you're absolutely right. I wasn't trying to suggest it's the only ternary operator, using `the` instead of `a` was a mistake, but it was a language usage mistake (not a native english speaker). But using `ternary operator` instead of `conditional operator` was indeed plain wrong. – yannis Nov 29 '11 at 09:47
  • I would worry about code correctness due to using short-circuiting rather than not using it before I consider any performance issues. The following statements are very different: `IIF(x is Nothing, Nothing, x.Length)` and `IF(x is Nothing, Nothing, x.Length)`. I believe that one of them will throw an exception when `x is Nothing`. – Job Nov 29 '11 at 23:04

4 Answers4

5

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
user281377
  • 28,352
  • 5
  • 75
  • 130
  • 2
    On [IIf efficiency](http://en.wikipedia.org/wiki/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 07:50
  • @ammoQ So its all about simplicity and readability and nothing to do with speed. – Maxood Nov 29 '11 at 07: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 07:54
  • @Maxood I don't say it, the article I link to does. IIf is [syntactic sugar](http://en.wikipedia.org/wiki/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 08: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 08:16
4

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.

tdammers
  • 52,406
  • 14
  • 106
  • 154
2

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

OmarOthman
  • 123
  • 8
  • 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 08:18
2

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.

Mark Booth
  • 14,214
  • 3
  • 40
  • 79