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 ?
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS Contact USThanks. We have received your request and will respond promptly. Come Join Us!Are you a
Computer / IT professional? Join Tek-Tips Forums!
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail. Posting Guidelines |
Which is faster if-else or Switch case ?
|
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.
RE: Which is faster if-else or Switch case ?
For example, if a number is 5, make it 2, if it is 2, make it 5. It could be coded as
CODE
RE: Which is faster if-else or Switch case ?
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 ?
If the cases are sequential, very often, the implementation is the equivalent of a computed goto. For example
CODE
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 ?