|
Rob Thompsonrob.sun3.org |
<?
$var=rand(0,1000000);
if ($var==0)
{
echo "found 0";
}
elseif ($var==1)
{
echo "found 1";
}
elseif ($var==2)
{
echo "found 2";
}
....
?>
<?
$var=rand(0,1000000);
if ($var==1)
{
echo "found 1";
}
if ($var==2)
{
echo "found 2";
}
if ($var==3)
{
echo "found 3";
}
....
?>
<?
$var=rand(0,1000000);
switch ($var)
{
case 1:
echo "found 1";
break;
case 2:
echo "found 2";
break;
case 3:
echo "found 3";
break;
....
?>
01/27/2008 10:06am
Interesting test! But it's a bit obivous. In the "If,if" case you have to test 1,000,000 conditions. In the workst case the same is true for the "If-Elseif", in a statistical way you test 500,000 conditions for the "If-Elseif" construct every time. I'm not sure about php internals, but usually for switch you have sequential access and so only one test (or jump), so here you have the reason why switch is much faster in this case. But usually you haven't as many conditions so the result may not have a big practical relevance.
You can optimise the if conditions by using === instead of ==.
Marc Gear on 01/28/2008 06:03am
Don't forget that you can switch(true) and check conditionals in your case statements:
switch (true)
{
case ($a = 0):
echo 'zero';
break;
// etc
}
ApeHanger on 01/29/2008 04:01am
> case($a = 0):
should be case ($a == 0):
otherwise $a is 0 from this assignment.
Better way to write the comparisons is
case (0 == $a):
so the interpreter would warn you, if you try to assign (0 = $a) a value to a constant.
ApeHanger
I've done a lot of benchmarking several things lately. One of these things is the switch against the if statement.
I came to the same conclusion and therefore use the switch statement a lot more nowadays. It is easier to read too.
It is also worth to mention that putting your most obvious match on top of the list (like default:) and then the second most obvious match etc. This makes the switch exit as soon as possible leaving the other matches not needed anymore.
Rewriting your scripts on the right spots can be a huge win if you take these things in mind. Even if you make complex switch cases, it is really a win.
Another topic is the foreach against a for loop etc.
I never use the foreach loop actually, only if it is clearer in my mind. I rather do this with an array:
$blah = array(0 => 'blah', 1 => 'blah');
$blah_keys = array_keys($blah);
$blah_vals = array_values($blah);
$blah_cnt = count($blah);
for ($i = 0; $i < $blah_cnt; $i++) {
$key = $blah_keys[$i];
$val = $blah_vals[$i];
}
One would think it is much more code. True. Most of the time you only need $blah_vals anyway. But it is so much faster in most cases.
You can also do other funny benchmarking. Like finding out if defining a constant really helps and if it doesn't clugs the memory. Or finding out how fast variable variables are or re-asigning variables with different sizes and types. So much to find out...
- Unomi -
Josh Johnston on 02/01/2008 08:30pm
Micro optimizations are great and all, but they are no substitute for optimizing the real bottlenecks: Databases and File I/O
Robert on 02/03/2008 06:14am
what bugs me about switch statements is the lack of curly bracket syntax for the individual cases, which i find easier to read at a glance:
switch($a)
{
case(10)
{
echo("omg");
}
case(20)
{
echo("wtf");
}
case(30)
{
echo("bbq?");
}
}
I'm having a hard time wrapping my mind around the logic behind "switch (true)". Can someone help me understand why that works?
Thanks!
Shane
Write a comment
* = required field