Re: IF or SWITCH

From: Date: Fri, 07 Apr 2006 03:55:44 +0000
Subject: Re: IF or SWITCH
References: 1 2 3 4  Groups: php.general 
Request: Send a blank email to php-general+get-233414@lists.php.net to get a copy of this message
On Thu, 2006-04-06 at 22:40, Kevin Waterson wrote: > This one time, at band camp, Robert Cummings <robert@interjinn.com> wrote: > > > I'm gonna go out on a limb here and say WRONG! > > > > Run yourself a benchmark. > > benchmarks can be hazardous, but lets look at them at their most basic level. By this > I mean how folks use them every day... > > http://www.phpro.org/benchmarks/if-switch-benchmark.html Hazardous is right. That's a terrible benchmark. Could have eliminated hundreds of sources of skew by running the iterations at the command line. It even does an echos *lol*. I ran the following as two shell scripts, 10 times each and averaged the times to get that switch is faster by 0.000000122 secs. Big whup, as I said in a previous post, that's very likely due to the precomputation being assigned in userland PHP versus the internal engine. As for which one is best, that's just flamebait -- right up there with preferred braces style, tabs or spaces to indent, top post versus bottom post *teehee*, etc. Personally I prefer if/elseif/else for a moderate number of conditions or if the conditional expressions are very complex. Switch I generally use for a large set of constants. They both have readability pros and cons. Cheers, Rob. <?php for( $i = 0; $i < 1000000; $i++ ) { $foo = 3; if( $foo == 1 ) { } else if( $foo == 2 ) { } else if( $foo == 3 ) { } else if( $foo == 4 ) { } else { } } ?> <?php for( $i = 0; $i < 1000000; $i++ ) { $foo = 3; switch( $foo ) { case 1: { break; } case 2: { break; } case 3: { break; } case 4: { break; } default: { } } } ?> *Wheeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee* ;) -- .------------------------------------------------------------. | InterJinn Application Framework - http://www.interjinn.com | :------------------------------------------------------------: | An application and templating framework for PHP. Boasting | | a powerful, scalable system for accessing system services | | such as forms, properties, sessions, and caches. InterJinn | | also provides an extremely flexible architecture for | | creating re-usable components quickly and easily. | `------------------------------------------------------------'

« previous php.general (#233414) next »