Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP why is continue n slower than using break
    text
    copied!<p>Please consider the following code:</p> <pre><code>$start = microtime(); for($i = 2; $i &lt; 100; $i++) { for($y = 2; $y &lt;= sqrt($i); $y++) { if($i%$y != 0) { continue; } else { continue 2; } } echo $i.','; } echo "\nFinished in " . (microtime() - $start); </code></pre> <p>Given that the above code effectively uses continue 2 to break the inner loop and skip any code post the inner loop, why does the following code on average execute faster when it appears to do more:</p> <pre><code> $start = microtime(); for($i = 2; $i &lt; 100; $i++) { $flag = true; for($y = 2; $y &lt;= sqrt($i); $y++) { if($i%$y != 0) { continue; } else { $flag = false; break; } } if($flag === true) echo $i.','; } echo "\nFinished in " . (microtime() - $start); </code></pre> <p>Thanks for any input.</p> <p><em><strong></em>__<em>_</em>__<em></strong> Update <strong></em>__<em>_</em>__<em>_</em>__<em>_</em>__<em>_</em></strong></p> <p>Thanks for the feedback but we seem to have missed the point. Regardless of if this is good programming practice I was trying to understand why the performance difference (which is tiny but consistent) is not within the bias I expected.</p> <p>The passing of true to microtime seems insignificant as both samples are measured using the same method with the same overhead and the same inaccuracy.</p> <p>More than one run was tested, as implied by use of the word average.</p> <p>Just for illustration please consider the following small samples using microtime(true) which shows the same pattern as using microtime().</p> <p>I know this is a small sample but the pattern is quite clear:</p> <p>Continue 0.00037288665771484 0.00048208236694336 0.00046110153198242 0.00039386749267578 0.0003662109375</p> <p>Break 0.00033903121948242 0.00035715103149414 0.00033307075500488 0.00034403800964355 0.00032901763916016</p> <p>Thanks for looking, and thanks for any further feedback.</p> <p><em><strong></em>__<em>_</em>___<em></strong> UPDATE Further investigation <strong></em>__<em>_</em>__<em>_</em>__<em>_</em>___</strong></p> <p>Interestingly if the echo statements are removed from the code the continue performs faster, with the echo statements in place break is faster.</p> <p>Please consider the following code sample, and consider that the results are in conflict dependant on if the echo statements are removed or not:</p> <pre><code>&lt;?php $breakStats = array(); $continueStats = array(); ob_start(); for($i = 0; $i &lt; 10000; $i++) { $breakStats[] = doBreakTest(); $continueStats[] = doContinueTest(); } ob_clean(); echo "&lt;br/&gt;Continue Mean " . (array_sum($continueStats) / count($continueStats)); echo "&lt;br/&gt;Break Mean " . (array_sum($breakStats) / count($breakStats)); function doBreakTest() { $start = microtime(true); for($i = 2; $i &lt; 100; $i++) { $flag = true; $root = sqrt($i); for($y = 2; $y &lt;= $root; $y++) { if($i%$y != 0) { continue; } else { $flag = false; break; } } } if($flag === true) echo $i . ''; return microtime(true) - $start; } function doContinueTest() { $start = microtime(true); for($i = 2; $i &lt; 100; $i++) { $root = sqrt($i); for($y = 2; $y &lt;= $root; $y++) { if($i%$y != 0) { continue; } else { echo $i . ''; continue 2; } } } return microtime(true) - $start; } </code></pre> <p>Echo statements present :</p> <p>Continue Mean 0.00014134283065796 Break Mean 0.00012669243812561</p> <p>Echo statements not present :</p> <p>Continue Mean 0.00011746988296509 Break Mean 0.00013022310733795</p> <p>Note that by removing the echo statement from the break and flag test we also remove the ($flag === true) check, so the load should reduce, but continue in this case still wins. W</p> <p>So in a pure continue n versus break + flag scenario, it appears that continue n is the faster contstruct. But add an equal number of identical echo statements, and the continue n performance flags.</p> <p>This makes sense to me logically that continue n should be faster, but I would have expected to see the same with the echo statements present.</p> <p>This is clearly a difference in the generated opcodes, and the position of the echo statement (inner loop vs outer loop) does anyone know a way of seeing the opcodes generated? This I suppose is ultimatley what I need as I am trying to understand what is happening internally.</p> <p>Thanks :)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload