Is there any performance difference in using
a = a <= b ? a : b;
versus
a = std::min(a, b);
In the code where I am working the first form is used but the variable names are quite long which makes it hard to read. I would prefer to use the second but not sure if there is any performance difference.
It is not faster. There is one difference when you can initialize a constant variable depending on some expression: const int x = (a<b) ?
Yes! The second is vastly more readable.
There is no difference in speed. Some prefer the if/else for readability. Personally, I use the ternary operator whenever the logic is trivial enough to understand on one line.
This is not branchless. It's if-less. But the ternary operator (?:) is a branching instruction.
I tested it with gcc -O2 and both produced the exact same assembly. There is no difference at all.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With