Fastest/Proper way of ordering if/else if statements
In PHP, is there a fastest/proper way of ordering if/else if statements? For some reason开发者_Go百科, in my head, I like to think that the first if statement should be the anticipated "most popular" met condition, followed by the 2nd, etc. But, does it really matter? Is there is a speed or processing time affected if the 2nd condition is the most popular choice (meaning the system must always read the first condition)
Ex:
if ("This is the most chosen condition" == $conditions)
{
}
else if ("This is the second most chosen condition" == $conditions)
{
}
else if ("This is the third most chosen condition" == $conditions)
{
}
Speedwise, it won't make a difference... Not a noticeable one... The order does count (it's sensibly faster putting the most used condition first), however it doesn't count for much. Choose the order which provides the best readability to those modifying and maintaining your code. They'll thank you later.
EDIT: Also, consider this:
My function will return with a 25% chance. I prefer writing:
if ( $chance25 )
return;
else if ( $chance40 )
doSomething();
else if ( $chance30 )
doSomethingElse();
else if ( $chance5 )
doSomethingElse2();
rather than:
if ( $chance40 )
doSomething();
else if ( $chance30 )
doSomethingElse();
else if ( $chance25 )
return;
else if ( $chance5 )
doSomethingElse2();
It's just nicer ordering by functionality...
EDIT2:
One size does not fit all. If your conditions are methods returning booleans, order them by how fast the method runs combined with the chance. I guess there's not really one good answer, you need to adapt. For example, if my $chance25 was replaced by a method reallySlowMethodDoNotUseUnlessYouReallyHaveTo()
, I would surely check it last. :D
I agree with what @Luchian. Your primary focus should be readability of the code
You should profile your application before you optimize your code. How you order your condition is highly dependent on how much time is spent in "each if condition".
Let's take an example:
Execution time - %ge called
Case 1 - 50 seconds (80% of time)
Case 2 - 10 seconds (15% of time)
Case 3 - 1 second (5% of time)
100 runs:
Order A (In the order of "how often a condition is executed")
Case 1, Case 2, Case 3 = (80 * 50) + (15 * 60) + (5 * 61) = 5205 seconds
Order B (In the order of "execution times")
Case 3, Case 2, Case 1 = (5 * 1) + (15 * 11) + (80 * 61) = 5050 seconds
Your application is probably a web application (since this is most popular usage of PHP), so most of the time it waits for external resources like database, file access or webservices. Unless you're doing it in a loop that's executed thousands of times, or in a recursive method, it won't really matter which condition goes first. Strive for code that's easy to read.
depends on your preference of operation
you might want condition two to activate rather than condition one and vice versa
$value=25;
if ($value > 20)
{
$value=200;
}
else if ($value < 50)
{
$value=5;
}
If you are simply checking the value of $conditions
against multiple possible text values, instead of doing if/else
, use switch.
switch ($conditions) {
case "This is the most chosen condition":
// do stuff
break;
case "This is the second most chosen condition":
// do stuff
break;
case "This is the third most chosen condition":
// do stuff
break;
default:
// do stuff
}
If the most common condition is first, it will not have to evaluate any other cases and will therefore be faster, but the difference is going to be so extremely small that it really isn't going to matter one way or another. Usually you should go for readability over speed.
精彩评论