Page 1 of 1

which is faster? If or Switch

Posted: Thu Oct 30, 2003 4:06 pm
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?

Posted: Thu Oct 30, 2003 4:27 pm
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

Posted: Thu Oct 30, 2003 4:57 pm
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; 
?>

Posted: Thu Oct 30, 2003 5:26 pm
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

Posted: Thu Oct 30, 2003 10:28 pm
by phice
Not to mention switch() is only limited to one variable, when if can go on foreverrrrrrrrrrr. :D

Posted: Fri Oct 31, 2003 4:42 am
by m3mn0n
Why not run some benchmark tests?

http://php.net/microtime

Posted: Fri Oct 31, 2003 7:27 am
by maniac9
all of these are really great ideas...thanks for the help!

Posted: Fri Oct 31, 2003 7:32 pm
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.

Posted: Fri Oct 31, 2003 8:38 pm
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