Reputation: 11448
Assume there is already an if statement on the page.
Now, out of:
a second if statement
else
else if
Which is fastest and which is slowest to run?
Does it matter if $a only takes 2 possible values?
edit: Sample code:
$a=2;
if ($a==1){
...
}
THEN
if ($a==2){
...
}
OR
else {
...
}
OR
else if ($a==2){
...
}
Upvotes: 1
Views: 134
Reputation: 54445
This isn't really an answer, but I have to wonder why you think this is important? (If you're worried about optimising things to this level, then I suspect PHP may not be the ideal language.)
In essence, code legibility, consistency and anything that will aid long term maintenance is of way more importance than such micro-optimisation and if you are experiencing performance issues, I'd happily place a large quantity of money (albeit someone else's money) on the fact that it won't be anything to do with if/else/switch, etc. code blocks.
As such, if you're having performance issues, profile the code to find out where the problems lie - don't waste time worrying about the "best" form of conditional statement layout. The "best" form is that which is most easily understood by the programmer(s) working on the project.
Upvotes: 4
Reputation: 3523
The else
will simply be the quickest as it has already calculated whether the if
statement is true
or false
. The slowest will be the else if
, as it has to called only if the if
is false
, and has to do an extra check afterwards. The normal if
is in the middle, just being an extra check.
You shouldn't base this choice on performance though, but rather on what logic your program should have.
Upvotes: 0
Reputation: 6679
While I admire your craving for speed, I think that measuring the benchmarks of PHP conditionals will be something that will never be conclusive because of the many different variables affecting your output.
With that said, this article shows benchmarks between the if/elseif/else statements as opposed to switch/case statements. It's very interesting to me that switch/case tends to be 15% faster than if/elseif/else.
Hope that helps you feed your uber, enterprise-level cravings!
Upvotes: 0
Reputation: 18859
Which is fastest and which is slowest to run?
I think the difference in speed would be negligible, but the latter would seem to be faster. The former solution will test two distinct conditions, whereas in the latter example, the condition in elseif wouldn't even be evaluated: that won't be necessary. So, I think this should be the fastest:
<?php
$foo = 42;
if( $foo === 42 ) {
}
elseif( $foo === 43 ) { // the condition isn't executed.
}
Upvotes: 0
Reputation: 449525
Unless you do a million comparisons on the same page (which is unlikely), it will absolutely, fundamentally not matter. Use whatever is best readable.
If you have many if
/ elseif
conditions, also take a look at the switch
statement.
Upvotes: 2
Reputation: 265341
using a separate if condition will probably be slightly slower than using else, but please, you won't be able to notice the difference even when having a million conditionals. if you really need the speed, then php is not the right choice anyway.
to be definite about the answer you'd have to do profiling of the different flavors of conditionals yourself.
Upvotes: 0