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

Ryan Boren ryan at boren.nu
Fri Nov 9 17:16:10 GMT 2007


On 11/9/07, Travis Snoozy <ai2097 at users.sourceforge.net> wrote:
> 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.

How about you patch it so that it is fast as can be on your setup, and
then I'll profile  the patch on my assortment of servers?

I've been doing some profiling against 2.4 and fixing up some
particularly slow places.  2.4 is quite a bit faster for me now.  If
you cachegrinders see anything that needs sped up, let us know and we
can start tweaking.


More information about the wp-hackers mailing list