dimanche 16 août 2020

Is ternary operator less efficient than an if statement that sets a different value to a variable

Example 1:

a = a > b ? b : a

Example 2:

if (a > b)
    a = b

While the difference may not be much, I'm thinking example 2 is computationally more efficient, as in example 1, if a < b, the same value of a is still put inside the variable a which is an unnecessary operation, one that's avoided in the if statement.

On the other hand, I'm thinking maybe the compiler understands this and both statements work with same efficiency post compilation because they correspond to the same instructions?

Aucun commentaire:

Enregistrer un commentaire