1

Is there any sort of performance difference (speed or memory, especially) between the following two approaches?

$foo = false;
for ($i = 0; $i < 100; $i++) {
    if ($i % 5 === 0) {
        $foo = true;
    }
}

vs.

$foo = false;
for ($i = 0; $i < 100; $i++) {
    $foo = $i % 5 === 0;
}

This is an abstract example, but while I prefer the more succinct syntax of the latter, I'm unsure if constant assignment impacts performance?

kaarch
  • 119
  • 1
  • 4
  • 10
  • 5
    If there is, it will be such a small difference that you never will notice it. Just go with the one you prefer. Don't start micro optimization before you have a clear reason to. And then, there's most likely other parts of your application that matters more. – M. Eriksson Feb 21 '19 at 16:44
  • Run it 5x in a loop with 1.000.000 repetitions to magnify performance diffs. More than performance is important readability - in which first example clearly wins. – lubosdz Feb 21 '19 at 16:46
  • 2
    Don't chose on behalf of a nanosecond of "performance". Chose which variant is better readable and understandable by others, which is the real significant performance indicator in 99.99999% of the use cases. – Daniel W. Feb 21 '19 at 16:46
  • 2
    I fail to see why you couldn't benchmark this yourself and find out. You woulld need at least 1,000,000 iterations to see any consistent difference though. You would gain more performance via `++$i` instead of `$i++` – MonkeyZeus Feb 21 '19 at 16:46
  • 2
    What @DanFromGermany said. If you choose the latter then at least make it less cryptic with parenthesis: `$foo = ($i % 5 === 0);` – MonkeyZeus Feb 21 '19 at 16:47
  • 1
    I like the second one, and you don't need `$foo = false;` – AbraCadaver Feb 21 '19 at 16:48
  • 5
    By the way, your two scripts don't do the same and they behave completely different ;-) The second overwrites the value each time, the first one only in case the if condition matches. – Daniel W. Feb 21 '19 at 16:49
  • @DanFromGermany - I know, the behavior in this example is not important, my question is exactly what you stated. Does overwriting the value each time have performance implications that assigning it only when a condition matches doesn't? Essentially, the variable exists and is only ever a boolean. The conditional is run every time in both cases. But does doing the assignment every time vs. only doing it if the condition matches "cost" more? – kaarch Feb 21 '19 at 16:54
  • 1
    @DanFromGermany I thought something was fishy about that code snippet. Adding `else{ $foo = false; }` to the former should make them behave logically identical. – MonkeyZeus Feb 21 '19 at 16:54
  • @kaarch as MonkeyZeus said, do your own benchmark. But it's useless IMO, PHP's performance is slightly different in each PHP release and environment. Before doing a single micro optimization, I'd rather upgrade the server and give it more power. – Daniel W. Feb 21 '19 at 16:56
  • All these comments are great! I'm aware that any difference in the given example would be indistinguishable, but I want to learn the low level underlying concepts of PHP itself to better understand in other circumstances if one approach would be better than the other. – kaarch Feb 21 '19 at 16:57
  • @kaarch I beg to differ. Even though your example is unimportant, if you choose a programming syntax solely based on performance and not verifying that it behaves as expected then the fastest, most optimized code in the world is thoroughly useless. – MonkeyZeus Feb 21 '19 at 16:57
  • @kaarch Is your general question "How do I benchmark PHP code?" If yes, then check out https://stackoverflow.com/q/8291366/2191572 and solve the mystery yourself. – MonkeyZeus Feb 21 '19 at 16:58
  • @MonkeyZeus - The point I'm hoping to understand is *how* the language works. Like I said, the variable exists in memory and the conditional happens on every iteration in both examples. The part that I don't know is if there is any additional "cost" to reassigning a variable once it's been set. – kaarch Feb 21 '19 at 17:00
  • @MonkeyZeus Not benchmarking, specifically, just understanding what's happening in either case, so that in less abstract examples there might be a reason to choose one approach over the other. – kaarch Feb 21 '19 at 17:01
  • 2
    @kaarch to understand `how` the language works, you may **[look at my answer here](https://stackoverflow.com/questions/54651290/which-is-faster-between-stringvalue-and-value-when-casting-to-a-string/54654067#54654067) ** to a vary similar question, and follow a similar approach. And i cannot stress how much optimize code on that level tends to be futile , **and** that in your attempt to measure a difference between two approaches you must be certain that they both have the same outcomes/side effects. – YvesLeBorg Feb 21 '19 at 17:02
  • @YvesLeBorg awesome! Thanks for that answer - that's the concept I'm trying to understand. I agree that this kind of "optimization" is not important in many cases (my example especially), but knowing the reason/how behind what PHP is doing will help form opinions in places where it would make a meaningful difference. – kaarch Feb 21 '19 at 17:06
  • @kaarch ... glad it helped. Dont forget Knuth's law : "We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil" ~~ D. Knuth , still very true and pertinent more than 50 years later. – YvesLeBorg Feb 21 '19 at 17:08

0 Answers0