When I’m teaching “C Programming for Embedded Systems”, I’m often asked whether the ternary operator should be used or an if/else statement. Hidden behind the question is really the need to know whether the ternary operator is more efficient than if/else. One might think that compilers today would generate identical code. Let’s take a quick look at a simple comparison and see if we can discern the differences between the two.
First, let’s identify two equivalent statements to compare. Below is some code that does the exact same thing except on the left we have the ternary operators’ version and on the right the if/else statements implementation.
Both statements will mask Status.Data with 0x10 and if the bit is set, Status.Data will be cleared otherwise bit 0 is set. Now in a real application this might be a nonsensical operation but it at least gives us a test bed to see the code that is generated by the compiler. In this case, I was using GCC for an ARM Cortex-M processor with all the default optimization levels. The compiler spits out the following code (with my own highlighting):
From a quick, one might suggest that using the ternary operator is more efficient but let’s stop for a moment and really look at what is happening. First, using the if/else statement overall does generate more instructions, approximately three extra instructions. One might expect then to find that code written completely with if/else statements would use slightly more flash space than code that exclusively uses the ternary operator. Three instructions though really aren’t that much in the grand scheme of things especially considering how much easier it is to read and understand an if/else statement over the ternary operator.
Second, if one were to actually follow the executable path through the if/else statement, for a given result, the if/else statement actually has fewer instructions to execute compared to the ternary operator. One might consider then that the if/else statement while code size wise is slightly larger, the if/else statement will execute slightly faster since either execution path contains fewer overall instructions to execute.
What gets even more interesting is that if we start to bump up the optimization level, we find that the if/else instructions can actually be optimized slightly to get the following result:
Code size wise, the two are essentially equivalent, yet each branch of the if/else statement will execute fewer instructions which should result in faster execution.
We can use both the ternary operator and if/else statement to get equivalent results in our code. Many developers are tempted to believe that the ternary operator is more efficient and in some situations and with certain compilers and optimization settings it very well can be. However, reading a ternary statement can often be confusing and error prone when compared to if/else statements. I always use if/else over the ternary operator simply due to the difference in code readability. From this quick look under the hood at what the compiler is doing, we can see that there doesn’t necessarily have to be a performance or code size hit to use if/else statements.
Errrm… sorry to be a party pooper, but haven’t you got the operator precedence completely wrong in your example? What you should have written was:
((Status.Data & 0x10) == 0x10)
What you wrote was
(Status.Data & 0x10 == 0x10)
which C’s precedence order turns into
(Status.Data & (0x10 == 0x10))
(Status.Data & 1)
The real mystery here is why your compiler produced such terrible code for something so ridiculously simple. :-/
Agreed! Perhaps sometime in the future, I’ll revisit this example.