I'd like to compare the traditional if statement with that of the immediate if statement (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 immediate if statement (IIF)?
|
|
||||
| show 3 more comments |
|
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 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:
vs.
or
|
|||||||||||||
|
|
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:
The same code written with an explicit That said, |
|||
|
|
|
@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:
which can't be written as
But rather split into more than one line. This will be noticeable as the logic inside the if/else grows. |
||||
|
|
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 Finally, you have to be very careful about using the VB |
|||
|
|

? :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