Importance of performance
PHP is a language where performance is not important, it is slow in much of its operations. So keep in mind that all information here is only relevant as curiosity. The cost of processing one or the other is very low for everything you want to do with PHP. If you need more performance yet, another language should be used.
Validity of performance tests
Also by way of curiosity, can be seen another answer that I posted about C#. Ali shows that in that language the code of the two forms produce the same bytecode (intermediate code) and must produce the same native code. So there is no difference in performance. So each situation is different.
In PHP it could happen exactly the same thing, but it doesn’t happen.
Note that although some pseudo programmers find it possible to compare performance of programming languages or even different mechanisms of the same language, this is not possible. You can even joke, but you can’t do serious conclusive studies. Some more conscious will even say that you cannot compare languages because they are abstract and have different versions. Then it would be possible to compare implementations which is something more concrete. Nor is this possible.
Even comparing hypothetical situations should be considered with a pinch of salt. You can compare real algorithms used in real situations. Then I’ll go compare algorithms and not exactly the conditional operator versus the conditional command. I will compare a broader processing using these mechanisms. Nor is it possible to compare only them. I will compare something that does much more than these operations.
It is also good to realize that I will run the test in an environment that is not ideal. Other factors may influence the result running on ideone. In addition what works well running on one machine can run differently on another. I don’t think this will happen with this test in PHP, but remember this in any benchmark what to see. This is one more reason that prevents us from trusting in these tests.
The test
See the test on ideone. And in the repl it.. Also put on the Github for future reference.
Run a few times to see that gives difference.
$inicio = microtime(true);
for ($i = 0; $i < 10000; $i++) {
$var = $i;
}
echo (microtime(true) - $inicio) * 1000 . "\n";
$inicio = microtime(true);
for ($i = 0; $i < 10000; $i++) {
$var = $i == 500 ? 0 : $i;
}
echo (microtime(true) - $inicio) * 1000 . "\n";
$inicio = microtime(true);
for ($i = 0; $i < 10000; $i++) {
if ($i == 500) {
$var = 0;
} else {
$var = $i;
}
}
echo (microtime(true) - $inicio) * 1000 . "\n";
Note that what really takes time is other things. It is the control of the loop with all the necessary operations in it - increment, comparison, decision and deviation - in addition to the expense of assigning a value to the variable.
I don’t know what optimizations PHP does and it may be that this test isn’t even ideal. I don’t think it does any relevant optimization. I’ve done some previous tests to try to figure out if there’s any optimization that would harm the test unduly, but there doesn’t seem to be. I could be wrong. If anyone can give information to help improve the test, it would help everyone. But I know that no test will be perfect. In theory we can create a perfect test but it is very difficult to meet all the conditions.
The first loop only executes the repetitions and does a normal assignment to know how long it takes to do this. The second obviously uses the conditional and the third can see that uses the if
.
It is not curious that the if
was faster? Note that you can notice that the use of if
increases the total processing time by about 2 tenths. It may vary when you have it executed. And the ternary increases by 2 tenths. So we can roughly conclude that the ternary takes twice as long as the if
. But it’s not the time necessarily spent on the mechanism itself, it’s a measurement of the processing of this part of the algorithm.
Test relevance
But keep in mind that this is how it was used. Otherwise it could give another completely different result. Even so, have you noticed how this operation spends little time compared to all processing? The loop and assignment consume 10 times more time. At least that’s how it is in this version of PHP, on this computer.
I came up with a test running more than a simple variable assignment, something heavier, but not too heavy - something that would normally occur in real situation. The difference from one algorithm to another that in the test demonstrated is already in the range of 10%, dropped to less than 1%.
Notice how it’s not usually relevant? In real processing running on production systems, this difference is usually lower.
The execution of each of these operations took about 20 to 40 microseconds. It’s very little. Of course, compared to other native code languages that can do the same in the nanosecond house, this is tragic. But it’s good enough for PHP.
Completion
There is a clear gain from the conditional command in this situation. But I do not guarantee that it occurs at all. It is probable, but not guaranteed. The gain is small, especially if you consider it in a larger processing, which is the case with virtually any real code. The gain is so small that if it makes a difference in what your software needs, PHP is the wrong language.
Then the choice must fall on which is more readable, which better passes the intention. Remembering that short code is usually more readable, until it gets too crowded, then the size starts playing against. Short code should not be the goal, clarity and expressiveness, yes. Performance comes well after.
Only reinforcing that this test cannot be considered valid even between different versions of PHP, much less for other languages.
Is there anything I can improve on the answer, or the others?
– stderr
Hello @qmechanik, all the answers are very enlightening and detailed, I left a time without schedule to ensure that there was no one else to add something or not to "choose too fast" as I’ve been seeing on
meta
.– Eduardo Silva
Are you doing any study or are performing performance adjustments on any real web application?
– Intruso
Hello @Intruder, I’m finalizing the development of my TCC this year, I did not have the habit of using ternary operators and now that I’m starting I have arisen the question of whether my gain was only in readability or in something more.
– Eduardo Silva
This type of optimization is usually not done in most applications because in the web, the cost elsewhere is actually more significant. You can take a look at this guy’s material to better understand what I’m talking about: https://www.stevesouders.com/
– Intruso