|
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
Shane-
Most people are used to writing "switch" blocks using a variable as the subject and literals as the "case" labels, as in
switch ($a) {
case 0: ... break;
case 1: ... break;
}
just like most people are used to writing tests as
if ($a == 0) { ... }
But as ApeHanger pointed out above, you can also write
if (0 == $a) { ... }
and by the same token, you can write
switch (0) {
case $a: ... break;
case $b: ... break;
}
or even
switch (true) {
case ($a == 1): ... break;
case ($b == 5): ... break;
}
The premise is, the subject of a "switch" and the argument of each "case" are treated like two sides of an ordinary comparison ("=="), so they are allowed to be expressions, not just literals or single variables.
Very interesting..
I guess it just comes down to personal preference and what looks best on screen, or what is easiest to follow in your code.
I use a LOT of IF IF statements, which I am looking to convert to case, just seems to be a lot cleaner, but the processing is so fast from PHP that it doesnt make much of a difference... and like Josh Johnston said on 02/01/2008, database bottlenecks and I/O is probably more likely to be your point of failure.
Anyone done tests on other PHP functions in the same regard as this article?
Cheers,
MickG
Tim on 10/30/2008 09:13am
The double and single quote got me wondering, so I tried this test running it four times, then deleting 10 low times and ten high times to weed out some server BS.
Double and single quotes come out the same without a variable to process.
Double Quotes with a variable took: 61.914114 seconds for 400 loops
Double Quotes took: 26.311353 seconds for 400 loops
Single Quotes took: 26.334875 seconds for 400 loops
PS I figured part of the server BS was the math on microtime is flaky and needs some tuning, if it goes into the next second it would create a negative result.
johnny on 01/01/2009 06:35am
mI2IrP Thanks for good post
wkatarina1969 on 01/04/2009 07:33am
Hi! A Great Post. I was just playing with blogs. i was really excited.
OpenSource on 01/06/2009 11:24am
lol ehehe i didn't notice that switch and If else statemets can be of same use in some situations. anyway thanx for the post 'ill show this to my friends...
by the way guys i've done a compilation of scripts and made a website full of php scripts please visit it. its free http://www.zmartscript.co.cc
ehrrlidbq on 05/04/2009 11:02am
sGM7jm jxxchryncujn, [url=http://wcuicemomnvz.com/]wcuicemomnvz[/url], [link=http://hfnojgfyrntg.com/]hfnojgfyrntg[/link], http://mnogrwsnnkpn.com/
Write a comment
* = required field