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?

Maxood
  • 1,493

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,434
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,936
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
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,352