Re: foreach() faster than while()?
| From: | (Chris Adams) | Date: | Wed, 14 Mar 2001 06:50:43 +0000 |
| Subject: | Re: foreach() faster than while()? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-43781@lists.php.net to get a copy of this message | ||
On 13 Mar 2001 18:10:45 -0800, Yasuo Ohgaki <yohgaki@hotmail.com> wrote:
>It seems foreach() operates much faster than equivalent while() loop.
>This is result is not what I expect. Foreach() operates on copy of array and I
>thought it would work a little slower than while().
Copies aren't necessarily copies - Zend includes a feature where a new variable
will point at the old one until something changes - if all you're doing is
reading that data, that never happens and it never needs to copy the actual
data.
Your while loop does two variable assignments in each loop and I doubt the
underlying engine is smart enough to play the same trick with the variables in
a list() assignment. Even if it was, it'd still have the difference of doing it
twice with whatever additional overhead list() imposes.
Disclaimer: This is all speculation. I could quite easily be wrong. If you want
a sure answer, take a look at the source.