Advertisement

Quick question...

Started by February 21, 2000 01:38 AM
2 comments, last by Kavos 25 years ago
In the end which is faster, if-else or switch? Thanks -Trev
Used correctly neither is really faster. Once you hit machine code it all turns into conditional branches and jumps anyway. However, I''m guessing switch statements are more idiot proof from a performance standpoint (not a program correctness standpoint.)
Advertisement
Yes, but which creates more branches and jumps?

-Trev
Ok, the following code:
switch (c) {  case 1:    a = 1;    break;  case 2:    b = 1;    break;  default:    c = 1;} 

Contains the same number of branches and jumps as the functionally equivalent:
if (c == 1) {  a = 1;} else if (c == 2) {  b = 1;} else {  c = 1;} 

Use which ever works best for you at the time. It''s all the same to the clam.

This topic is closed to new replies.

Advertisement