which is faster? If or Switch

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
maniac9
Forum Commoner
Posts: 55
Joined: Fri Aug 01, 2003 12:27 am
Location: Arkansas, USA
Contact:

which is faster? If or Switch

Post by maniac9 »

For this situation, I am passing soemthing to an index page - in the form index.php?page=blah or index.php?action=blah

Currently, I have each page or action in a function and quite a few if/else if statements that are calling the appropraite function for each page - about 30 total

My question is, would it be faster to put all of this in a swtch statement, or keep it in the existing if structure?
Cruzado_Mainfrm
Forum Contributor
Posts: 346
Joined: Sun Jun 15, 2003 11:22 pm
Location: Miami, FL

Post by Cruzado_Mainfrm »

switch is good if u have a few cases, like 3 or so, if not, make an array with the possible queries, like this:

Code: Select all

array (
 ї0] = "/mypage.php"
 ї1] = "/yourpage.php"
)

OR

array (
 їhome] = "/index.php"
 їsupport] = "/support.php"
 їabout] = "/about.php"
)
check for the match of a key and then get it's value if the key exists
and then use it for your purposes
User avatar
Kriek
Forum Contributor
Posts: 238
Joined: Wed May 29, 2002 3:46 am
Location: Florida
Contact:

Post by Kriek »

In most cases it is fifteen percent faster to use switch/case/break instead of if/elseif/else. Of course this depends on your application and individual code results do very. You could always combine them as well:

Code: Select all

<?php 
    $page = $_GET&#1111;'page']; 
    switch($page) &#123; 
        case $page: $page .= '.php'; 
        if(file_exists($page)) &#123; 
            include_once($page); 
        &#125; else &#123; 
            include_once('main.php'); 
        &#125; 
        break; 
    &#125; 
?>
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post by BDKR »

There is also a clarity issue. A switch is going to be easier to read and maintain (not to mention just grok in general) as opposed to a swarm of if statements.

Er..., I forgot to mention that after a certain number of if statements, it get's to be impossible to deal with the convuluted logic.

Cheers,
BDKR
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post by phice »

Not to mention switch() is only limited to one variable, when if can go on foreverrrrrrrrrrr. :D
Image Image
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

Why not run some benchmark tests?

http://php.net/microtime
maniac9
Forum Commoner
Posts: 55
Joined: Fri Aug 01, 2003 12:27 am
Location: Arkansas, USA
Contact:

Post by maniac9 »

all of these are really great ideas...thanks for the help!
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

BDKR wrote:There is also a clarity issue. A switch is going to be easier to read and maintain (not to mention just grok in general) as opposed to a swarm of if statements.

Er..., I forgot to mention that after a certain number of if statements, it get's to be impossible to deal with the convuluted logic.

Cheers,
BDKR
that is extremely arguable. especially considering i find if MUCH easier to follow then checking case and searching for break as the code that's executed for the case gets larger.


i also find i often test for multiple things at once, something switch cannot handle.
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post by BDKR »

m3rajk wrote: that is extremely arguable. especially considering i find if MUCH easier to follow then checking case and searching for break as the code that's executed for the case gets larger.

i also find i often test for multiple things at once, something switch cannot handle.
I feel that more often than not, the answer is in the details. The detail in this case is
...i find if MUCH easier to follow then checking case and searching for break as the code that's executed for the case gets larger.
In this case, I agree with you 100%.

It's my opinion that the clarifying benefits of a switch are thrown out the window when the actual code behind a particular case isn't abstracted away behind a function or method call(s). As an example,

Code: Select all

switch ($i) 
    {
    case 0:
        // some code
        // some code
        // some code
        // some code
        // some code
        // some code
        // some code
        // some code
        break;
    case 1:
        // more code
        // more code
        // more code
        // more code
        // more code
        // more code
        // more code
        // more code
        // more code
        // more code
        // more code
        // more code
        break;
    }
as opposed to

Code: Select all

switch($k)
    {
    case 0:
         { 
         $obj->some_code(); 
         break;
         }

    case 1:
         { 
         more_code(); 
         break;
         }
     }
In other words. A switch can be exceptionally clear and easy to read as long as we don't clutter it up. Take all of that code out of the switch and see how much easier it get's to deal with.

Cheers,
BDKR
Post Reply