which is faster? If or Switch
Moderator: General Moderators
which is faster? If or Switch
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?
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
switch is good if u have a few cases, like 3 or so, if not, make an array with the possible queries, like this:
check for the match of a key and then get it's value if the key exists
and then use it for your purposes
Code: Select all
array (
ї0] = "/mypage.php"
ї1] = "/yourpage.php"
)
OR
array (
їhome] = "/index.php"
їsupport] = "/support.php"
їabout] = "/about.php"
)and then use it for your purposes
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ї'page'];
switch($page) {
case $page: $page .= '.php';
if(file_exists($page)) {
include_once($page);
} else {
include_once('main.php');
}
break;
}
?>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
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.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
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 ism3rajk 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.
In this case, I agree with you 100%....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.
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;
}Code: Select all
switch($k)
{
case 0:
{
$obj->some_code();
break;
}
case 1:
{
more_code();
break;
}
}Cheers,
BDKR
