NYCPHP Meetup

NYPHP.org

[nycphp-talk] Switch-Case v. if/else

oorza2k5 at gmail.com oorza2k5 at gmail.com
Thu May 14 21:27:01 EDT 2009


oops, just sent this to one guy =\

On May 14, 2009 3:52pm, oorza2k5 at gmail.com wrote:
> Alright! answers :P

> Profiling the code, this script (which bottlenecks on the switch-case) is  
> 82% of execution time __with__ APC. Of the script itself, ~45% of the  
> execution time is this particular switch-case.

> I can't send the code, NDA's and whatnot, but it was basically (these are  
> all string comparisons that can't be refactored into numerical  
> comparisons, I pulled two cases out into a loop for that):

> if($) {

> } elseif ($b || $c || $d || $e) {

> ) elseif ($f || $g) {

> } else {

> }

> After changing it to

> switch($condition) {

> case 'a':
> blah;
> case 'b':
> case 'c':
> case 'd':
> case 'e':
> blah
> case 'f':
> case 'g':
> blah
> default:
> blah

> that conditional is about 5% faster.

> The script itself isn't slow, it's that it's an autoloader that has to be  
> ran 15-30 times per pageload and there's no viable way to make the  
> necessary infrastructure changes to remove it, nor is it viable to  
> require() manually, as that would take even more time. Before anyone  
> suggests, the original __autoload() was >90% of page time, and I split it  
> into an autoloading class and registered 4 different methods on the  
> autoloading stack ordered, of course, in order from most->least used.

> One of the things I was thinking about trying was keeping track of  
> classnames and paths in an internal array and serializing that resulting  
> array and pushing it into and pulling it out of APC (in the destructor  
> and constructor, respectively) and the first method on the autoloading  
> stack would check for $this->includeMap[$className] but I'm unsure if  
> that would be a sane approach or not.

> On May 14, 2009 5:38pm, John Campbell jcampbell1 at gmail.com> wrote:
> > PHP should run ~1M switch tests per second on decent hardware. Either
> >
> > you are misinterpreting you profiling data, or running a switch
> >
> > statement a hell of a lot of time. I can't imagine any sort of
> >
> > if/else vs switch vs. jump table is going to make much of a
> >
> > difference. At best you will see a speedup of 30% or so, but that
> >
> > won't really fix the underlying problem.
> >
> >
> >
> > On Thu, May 14, 2009 at 2:12 PM, Eddie Drapkin oorza2k5 at gmail.com>  
> wrote:
> >
> > > Does anyone know how the PHP Interpreter pulls switch/case statements
> >
> > > together? Does it emulate a C compiler and, for larger case sets,  
> build a
> >
> > > huge if/else cascade? Does it do this always? Is there any way to  
> know when
> >
> > > it builds a jump table (like as/c is supposed to)? I've got a slow  
> script
> >
> > > (it's eating ~85% of execution time) that I can't work around and one  
> of the
> >
> > > slower parts is a switch case (which is slightly faster than manually
> >
> > > building an if/else cascade) and was wondering if anyone had any  
> performance
> >
> > > tips for cases like these.
> >
> > >
> >
> > > _______________________________________________
> >
> > > New York PHP User Group Community Talk Mailing List
> >
> > > http://lists.nyphp.org/mailman/listinfo/talk
> >
> > >
> >
> > > http://www.nyphp.org/show_participation.php
> >
> > >
> >
> > _______________________________________________
> >
> > New York PHP User Group Community Talk Mailing List
> >
> > http://lists.nyphp.org/mailman/listinfo/talk
> >
> >
> >
> > http://www.nyphp.org/show_participation.php
> >
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.nyphp.org/pipermail/talk/attachments/20090515/529de153/attachment.html>


More information about the talk mailing list