Search code examples
perloptimization

Fact that perl ternary operator faster than if else


Is perl ternary operator faster than if else, if so by how many time, how to prove it?


Solution

  • if/else and the ternary operator are compiled into the same basic opcodes internally (OP_COND_EXPR). The only difference is that because the code inside the if/else blocks form a new scope, there is extra startup and teardown at the beginning/end of each block.

    But the performance difference is unlikely to be significant, and 99.9% of the time you should write the code in the way that maximises readability - for example, not using the ternary operator in void context.