Is else if faster than switch() case ?
For just a few items, the difference is small. If you have many items you should definitely use a switch.
If a switch contains more than five items, it's implemented using a lookup table or a hash list. This means that all items get the same access time, compared to a list of if:s where the last item takes much more time to reach as it has to evaluate every previous condition first.
Which is Faster and better, Switch Case or if else if?
Your first example is simply wrong. You need elseif
instead of just else
.
If you use if..elseif...
or switch
is mainly a matter of preference. The performance is the same.
However, if all your conditions are of the type x == value
with x
being the same in every condition, switch
usually makes sense. I'd also only use switch
if there are more than e.g. two conditions.
A case where switch
actually gives you a performance advantage is if the variable part is a function call:
switch(some_func()) {
case 1: ... break;
case 2: ... break;
}
Then some_func()
is only called once while with
if(some_func() == 1) {}
elseif(some_func() == 2) {}
it would be called twice - including possible side-effects of the function call happening twice. However, you could always use $res = some_func();
and then use $res
in your if
conditions - so you can avoid this problem alltogether.
A case where you cannot use switch at all is when you have more complex conditions - switch
only works for x == y
with y
being a constant value.
Is there any significant difference between using if/else and switch-case in C#?
SWITCH statement only produces same assembly as IFs in debug or compatibility mode. In release, it will be compiled into jump table (through MSIL 'switch' statement)- which is O(1).
C# (unlike many other languages) also allows to switch on string constants - and this works a bit differently. It's obviously not practical to build jump tables for strings of arbitrary lengths, so most often such switch will be compiled into stack of IFs.
But if number of conditions is big enough to cover overheads, C# compiler will create a HashTable object, populate it with string constants and make a lookup on that table followed by jump. Hashtable lookup is not strictly O(1) and has noticeable constant costs, but if number of case labels is large, it will be significantly faster than comparing to each string constant in IFs.
To sum it up, if number of conditions is more than 5 or so, prefer SWITCH over IF, otherwise use whatever looks better.
Case vs If Else If: Which is more efficient?
It seems that the compiler is better in optimizing a switch-statement than an if-statement.
The compiler doesn't know if the order of evaluating the if-statements is important to you, and can't perform any optimizations there. You could be calling methods in the if-statements, influencing variables. With the switch-statement it knows that all clauses can be evaluated at the same time and can put them in whatever order is most efficient.
Here's a small comparison:
http://www.blackwasp.co.uk/SpeedTestIfElseSwitch.aspx
Switch statement vs if statement, which is better for performance?
Switch may (depending on the implementatin) use jump table (in c and c++), so under certain conditions it may be more performant. Generally, it states your intention more clear so compiler can pretend it's smart.
considering if
in your example doesn't even use else if
chain, it makes switch
even faster (because of not evaluating all conditions) and also may change the meaning of your code (in case your dothises modify x
).
Javascript switch vs. if...else if...else
Answering in generalities:
- Yes, usually.
- See More Info Here
- Yes, because each has a different JS processing engine, however, in running a test on the site below, the switch always out performed the if, elseif on a large number of iterations.
Test site
Is there any performance gain from using a switch statement over a bunch of if()else if() in javascript?
In general, switch
is faster than if - else if
statements.
However, kind of best practice is to use if - else if
if you got max 3 conditionals. If you're going beyond that, you should use switch
statements.
The problem with if else
is that it possibly needs to check multiple times before it finally reaches the code to execute. Therefore you also need to optimize the order for your conditional statements.
if( foo ) {
}
else if( bar ) {
}
else if( baz ) {
}
That code would not make much sense from a performance prospective if you expect baz
to be true
and foo
/bar
to be false
most of the times.
C# if else vs if or vs switch case
In some cases, an equivalent switch statement is slower than an if-statement or chain of if-statements. Using frequency heuristics, you can optimize a fast path with an if-statement in many programs.
See this Link you will find two different comparisons
http://www.dotnetperls.com/if-switch-performance
Which is better a switch statement or if-else if-else statement?
The compiler can create a jump table for certain types of switch statements which is more efficient than just evaluating each element like a nested set of if statements. This is dependent on the type of switch and the language you are working in, but many C compilers just this sort of thing in their code generation.
So the short is that a switch can be more efficient but it depends on your particular usage.
Related Topics
Differencebetween the PHP Open Tags "<=" and "<Php"/"<"
How to Get Insert Id in Mssql in PHP
How Can One Run Multiple Versions of PHP 5.X on a Development Lamp Server
Scaling a Chat App - Short Polling VS. Long Polling (Ajax, PHP)
What Is the Equivalent of JavaScript's Encodeuricomponent in PHP
Call to Undefined Function Apache_Request_Headers()
Namespace in PHP Codeigniter Framework
Inserting into MySQL from PHP (Jquery/Ajax)
Access Denied for User @ 'Localhost' to Database ''
Php, Simplexml, Decoding Entities in Cdata
Get All Instances of a Class in PHP
How to Convert Boolean to String
Having Problems While Try to Install Oauth with Pecl in Mamp on MAC Os Lion