[wp-hackers] Foreach vs. each() (was: apply_filters...)

Travis Snoozy ai2097 at users.sourceforge.net
Fri Nov 9 10:21:52 GMT 2007


On Thu, 8 Nov 2007 22:26:47 -0800, "Ryan Boren" <ryan at boren.nu> wrote:

> On 11/8/07, Jacob <wordpress at santosj.name> wrote:
> > Hmm, I believe my usage of foreach was to use the $value of the
> > first foreach (since it is the array) instead of doing your method.
> > I was able to confirm that, at the very least, it doesn't break
> > anything on the trunk. I would rather use foreach than while since
> > it looks cleaner.
> >
> > If you want to take care of 2.3.x, I'll submit a patch for 2.4.
> > There are some other areas I want to clean up in the trunk.
> 
> I think we used to do a foreach.  A switch was made to do-while-next
> because it was supposedly faster.  I don't really care what the loop
> looks like as long it is as fast as can be.  apply_filters is called
> hundreds of times.  Any changes need to be profiled.

Yeah, I know -- that's how I got started down this whole path;
apply_filters was taking up a huge proportion of execution time. My
profiler shows reset(), each(), next() and ilk to be really darn slow;
switching to foreach cut the time spent in that function (directly)
about in half.

That said, I run Gentoo, and it could be that the tweak compiler flags I
used to build PHP cause different costs to be associated with these
constructs vs. PHP built with a different compiler/flags. So, I suggest
that you not take my word for it, and profile reset()/each() vs. foreach
yourself.


-- 
Travis 

In Series maintainer
Random coder & quality guy
<http://remstate.com/>


More information about the wp-hackers mailing list