Speed difference between If-Else and Ternary operator in C…?

后端 未结 6 2317
别跟我提以往
别跟我提以往 2020-12-01 16:49

So at the suggestion of a colleague, I just tested the speed difference between the ternary operator and the equivalent If-Else block... and it seems that the ternary operat

6条回答
  •  一生所求
    2020-12-01 17:39

    There's a good chance that the ternary operator gets compiled into a cmov while the if/else results in a cmp+jmp. Just take a look at the assembly (using -S) to be sure. With optimizations enabled, it won't matter any more anyway, as any good compiler should produce the same code in both cases.

提交回复
热议问题