Re: Re: [PEPr] Comment on Networking::Monitor

From: Date: Thu, 09 Dec 2004 19:14:56 +0000
Subject: Re: Re: [PEPr] Comment on Networking::Monitor
References: 1  Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-34973@lists.php.net to get a copy of this message
Bertrand Mansion wrote:
Joe Stump wrote:
It depends on the use. foreach() for small arrays is supposed to be faster than while(list() each()). Also, you don't have to reset() arrays after using foreach(), BUT foreach() makes a *copy* to work on, which can be resource heavy with really large arrays (ie. you'd have two copies of a 200k length array if you used foreach()). This is what I understand to be the differences. Things may have changed since I last looked into this. --Joe On Dec 9, 2004, at 10:04 AM, Robert Peake wrote:
Hi Bertrand, Yes -- I will double-check CS before moving to vote. s/if(/if (/g <- my best friend :) Why do you recommend foreach instead of while ... each?
     
As said by Joe, speed is the difference. With PHP5, you can now use reference for values instead of copying (but this will probably make your code not PHP4 compatible) as in foreach ($arr as &$value) So if the size of the array is not too big, feel free to use foreach instead :) Bertrand You might want to look at these benchmarks. It seems that foreach is faster in every case.
http://px.sklar.com/code.html?id=698 Oh and the code seems a bit messed up, you'll have to search and replace the extra \s out. -josh

« previous php.pear.dev (#34973) next »