|
Posted by Richard Lynch on 04/20/05 07:03
On Tue, April 19, 2005 7:03 am, Jochem Maas said:
> The 'other' guy mentioned that while() is faster than foreach,
> is this true?
Don't know ; Don't care.
You should never loop through so many things in PHP that it matters in the
first place :-)
> I read a few days ago somewhere on php.net that foreach() is the
> recommended (by php devs) way of iterating over arrays....
[shrug]
That's probably because they're tired of people not understanding the
internal pointer array, and asking FAQs about it. Or maybe not. Ask them
why they prefer it. I sure don't know.
> also, compare these 2 lines:
>
> while (list(, $idcat) = each($idcats)){ /* ... */ }
> foreach ($idcats as $idcat){ /* ... */ }
>
> now its seems to me that the foreach version is 'up' 2 function calls
None of those are function calls.
They are all language constructs. Okay, each() *might* be a function...
I'm not sure how much difference there is in the number of language
constructs used, nor if they are even comparable in sheer numbers the way
functions are.
foreach is probably slower, I think, because it creates a *copy* of the
array to work on, so it won't mess up the original and its internal
pointer.
Again, with 200 bytes, you are wasting your time to worry about any of this.
> on the while loop, all else being equal the foreach loop has to be faster
> (given that calling functions is relatively very expensive)...
> or is foreach() _really_ heavy when compared to while()?
Why don't you just benchmark it on your own machine and find out?
> not that I care too much, I find foreach() more pleasing to the eye and
> there is
> less to type (in the given example).
I'm an old dog, and I don't quite understand for sure how this new-fangled
foreach thingie works. I'd spend more time looking it up and reading
about it than just typing what I *know* works. [shrug]
--
Like Music?
http://l-i-e.com/artists.htm
Navigation:
[Reply to this message]
|