What is the fastest loop in php?

I have profiled for while and do-while loops with something simple:

while ($var < 1000000) { ++$var; } do { ++$var; } while ($var < 1000000); for ($var = 0; $var < 1000000; ++$var) { //do nothing } 

comparing microtime () before and after loops.

At a significant level, the do-while loop is the fastest loop. do-while is actually faster than almost half the time. I know that they are designed for different purposes (while checking the condition before the loop executes and does while it executes at least once).

I know that the general consensus is that while the loops frown and do, much less.

My question is why? Given how many loops are used in PHP applications, you should not do it — but can you use more? Even using the if statement to check the status before the loop executes, the performance boost will be significant.

My currently accepted answer is that code vulnerability is suspect.

+6
source share
3 answers
  • Microoptimization is evil . They decrease readability for no measurable performance gain. Even if your application has loops with millions of iterators (which, I doubt), the difference is still negligible.
  • The difference between while / do while less than what you say: http://codepad.viper-7.com/M8cgt9
  • To understand why do while works a little faster, look at the generated operation codes:

     line # * op fetch ext return operands --------------------------------------------------------------------------------- # while loop 3 0 > ASSIGN !0, 0 4 1 > IS_SMALLER ~1 !0, 1000000 2 > JMPZ ~1, ->5 3 > PRE_INC !0 4 > JMP ->1 5 > > RETURN 1 # do while loop 3 0 > ASSIGN !0, 0 4 1 > PRE_INC !0 2 IS_SMALLER ~2 !0, 1000000 3 > JMPNZ ~2, ->1 4 > > RETURN 1 # for loop 3 0 > ASSIGN !0, 0 1 > IS_SMALLER ~1 !0, 1000000 2 > JMPZNZ 5 ~1, ->6 3 > PRE_INC !0 4 > JMP ->1 5 > > JMP ->3 6 > > RETURN 1 

    There is only one jump statement ( JMPNZ ) in the do while loop, while two are needed for the while ( JMPZ , JMP ). The for loop requires three branch statements ( JMPZNZ , JMP , JMP ) and has generally more complex logic.

+15
source

If you need a fast loop, you have to deploy it or use a duff device.

You can also shorten the for-loop ( ) loop :

 for ($var = 0; ++$var < 10; ) { // do nothing } 

You can also shorten the do-while ( demo ) loop:

 $var=0; do { echo "Hello"; } while (++$var < 10); 

But the operation codes are the same.

And here is the modified version of the duff device from php.net:

 If you're already using the fastest algorithms you can find (on the order of O(1), O(n), or O(n log n)), and you're still worried about loop speed, unroll your loops using eg, Duff Device: <?php $n = $ITERATIONS % 8; while ($n--) $val++; $n = (int)($ITERATIONS / 8); while ($n--) { $val++; $val++; $val++; $val++; $val++; $val++; $val++; $val++; } ?> 

(This is a modified form of the original Duff device because PHP does not understand how the original egregious syntax is.)

This is algorithmically equivalent to the general form:

 <?php for ($i = 0; $i < $ITERATIONS; $i++) { $val++; } ?> $val++ can be whatever operation you need to perform ITERATIONS number of times. On my box, with no users, average run time across 100 samples with ITERATIONS = 10000000 (10 million) is: Duff version: 7.9857 s Obvious version: 27.608 s 
+1
source

If you are interested in such a thing, you can find PHPBench .

My personal opinion is that you should use while, do for loops where they are most readable. An increase of 6% in an empty cycle is not significant enough if you spend most of your time in the database.

0
source

Source: https://habr.com/ru/post/901219/


All Articles