Jump to content

"For" loop is more expensive, why?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
FireGator

FireGator

    Learning Programmer

  • Members
  • PipPipPip
  • 37 posts
I am confused as C, of which PHP is based on converts while loops and for loops to the same thing through optimization.

I had learned through various sources that when using while loops to iterate, in example decaring $i first, then using while($i <= 100) doSomething(); $i-- is what you should do as it functions the same way but does less steps?
>+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]
>++++++++[<++++>-] <.>+++++++++++

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
You are right, I had seen that a lot lately, I cannot find any explainations already on the web, so I will go into depth I guess..

PHP is an intepreted language of course. There are specific compiled opcodes assigned to the Zend engine which can give a better idea on what you are doing, the op codes can be found here: PHP: Opcodes - Manual. They are poorly documented, so I will have to explain them myself..

Your while loop example should produce something like this

$i = 0                 ;ASSIGN 0, exit statement
while($i <= 100) {     ;run IS_SMALLER_OR_EQUAL to 100
                       ;JMPZ, or Jump out of while loop if operation returns 0, exit statement
$int++                 ;POST_INC, exit statement
                       ;JMP to opcode 4, which is the IS_SMALLER_OR_EQUAL op code
}                      ;exit statement
                       ;location if loop is broken or $i <= 100
It will essentially jump back until the "smaller or equal" check will return zero, this is somewhat a "stupid" but efficient call.

Your for loop equivalent will be a bit longer, it should resolve to
for($i=0;        ;ASSIGN 0, continue
$i<=100;         ;IS_SMALLER_OR_EQUAL to 100
                 ;JMPZNZ, or a conditional jump.. if isequal+loop check satisfies
$i++) {          ;POST_INC
                 ;FREE (the old $i)
                 ;JMP to opcode 3 (IS_SMALLER_OR_EQUAL), exit statement. this is the loop body
}
                 ;JMP to opcode 5 (or final POST_INC portion, satisfies JMPZNZ), exit statement
A conditional jump in this manner will always be more complex, as you can see the for loop is not very optimized in the fact it uses multiple operations. You should really be pre incrementing your variables anyway, I believe the operation call is faster for the engine, as it does it during not after statement call.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
FireGator

FireGator

    Learning Programmer

  • Members
  • PipPipPip
  • 37 posts
This explains quite a lot about PHP to me, Thank you! I really do like that you explained why things just "are" what they are in PHP, there is a lot of mysticism involved then in its inner workings :)
>+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]
>++++++++[<++++>-] <.>+++++++++++