Pay attention to the following code:
$start = microtime(); for($i = 2; $i < 100; $i++) { for($y = 2; $y <= sqrt($i); $y++) { if($i%$y != 0) { continue; } else { continue 2; } } echo $i.','; } echo "\nFinished in " . (microtime() - $start);
Given that the code above uses continue 2 effectively to break the inner loop and skip any post of code in the inner loop, why does the following code run faster on average when it seems to do more:
$start = microtime(); for($i = 2; $i < 100; $i++) { $flag = true; for($y = 2; $y <= sqrt($i); $y++) { if($i%$y != 0) { continue; } else { $flag = false; break; } } if($flag === true) echo $i.','; } echo "\nFinished in " . (microtime() - $start);
Thanks for any input.
_____ Update ____________
Thanks for the tip, but we seem to have missed it. Regardless of whether this is good programming practice, I tried to understand why the difference in performance (which is tiny but consistent) is not within the bias that I expected.
The passage of truth in a microtiter seems insignificant, since both samples are measured using the same method with the same overhead and the same inaccuracies.
More than one run was verified, which was implied by using the middle word.
To illustrate, consider the following small samples using microtime (true), which show the same pattern as when using microtime ().
I know this is a small sample, but the template is pretty clear:
Continue +0.00037288665771484 +0.00048208236694336 +0.00046110153198242 +0.00039386749267578 +0.0003662109375
Break +0.00033903121948242 +0.00035715103149414 +0.00033307075500488 +0.00034403800964355 +0.00032901763916016
Thanks for watching, and thanks for the further feedback.
______ UPDATE Further research ____________
Interestingly, if echo statements are removed from the code, then continue is faster, and in-place echo statements are interrupted faster.
Please consider the following code example and consider that the results are in conflict, depending on whether the echo statements are deleted or not:
<?php $breakStats = array(); $continueStats = array(); ob_start(); for($i = 0; $i < 10000; $i++) { $breakStats[] = doBreakTest(); $continueStats[] = doContinueTest(); } ob_clean(); echo "<br/>Continue Mean " . (array_sum($continueStats) / count($continueStats)); echo "<br/>Break Mean " . (array_sum($breakStats) / count($breakStats)); function doBreakTest() { $start = microtime(true); for($i = 2; $i < 100; $i++) { $flag = true; $root = sqrt($i); for($y = 2; $y <= $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 < 100; $i++) { $root = sqrt($i); for($y = 2; $y <= $root; $y++) { if($i%$y != 0) { continue; } else { echo $i . ''; continue 2; } } } return microtime(true) - $start; }
Presented echo expressions:
Continue Average 0.00014134283065796 Break Mean 0.00012669243812561
Echo operators are not present:
Continue Average 0.00011746988296509 Break Mean 0.00013022310733795
Please note that by removing the echo instruction from the break and flag test, we will also remove the check ($ flag === true), so the load should decrease, but in this case, continue to win. Tue
So in a pure continuation scenario n and break + the flag seems to continue n is a faster counter. But add an equal number of identical echo operators and flags with a continuation of n.
It is logical that continue n should be faster, but I expected to see the same with echo messages present.
This is clearly the difference in the generated opcodes, and does the position of the echo statement (inner loop versus outer loop) do anyone know a way to see the generated opcodes? This, I believe, is the ultimatum that I need, as I try to understand what is happening inside the country.
Thanks:)