×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Which is faster if-else or Switch case ?

Which is faster if-else or Switch case ?

Which is faster if-else or Switch case ?

(OP)
Which is faster if-else or Switch case ?

RE: Which is faster if-else or Switch case ?

Depends on what you are doing - sometimes, it can be solved by simple arithmetic, which is faster than both. What exactly are you trying to do?

For example, if a number is 5, make it 2, if it is 2, make it 5. It could be coded as

CODE

' if statement - assuming it will only ever be 2 or 5
if x = 5 then
    x = 2
else
    x = 5
end if

' arithmetic - assuming it will only ever be 2 or 5
x = 7 - x 

RE: Which is faster if-else or Switch case ?

Technically, a Switch / Case statement is faster because in an IF Statment, each IF / ElseIF is evaluated. In a Switch/Case, once the condition is met, the Switch/Case is exited.
Personally, I find Switch/Case to look cleaner and more readable as well. If it is just a small check as in xwb's example, then I would just use a standard IF or to shorten it I would use an InLine IF. The syntax differs between VB and C#

RE: Which is faster if-else or Switch case ?

It really depends on the internal implementation of the switch statement.

If the cases are sequential, very often, the implementation is the equivalent of a computed goto. For example

CODE

switch (x)
    {
    case 8: ...; break;
    case 5: ...; break;
    case 7: ...; break;
    case 6: ...; break;
    } 
The compiler would arrange them as 5, 6, 7,8, subtract 5 from x and use a computed goto.

If the cases are not sequential, they could be sorted into a lookup table followed by a computed goto. Alternatively, it could be implemented as an if-elseif-else statement. If it is the latter then there is no difference between the switch and the if-elseif-else other than possibly elegance of the code.

RE: Which is faster if-else or Switch case ?

An IF statement will always evaluate each condition. A switch will fall through once the condition is met. True, depending on the conditions, they could be equal in performance, but the switch will win out in most cases.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close