Jump to content
  • Advertisement
Sign in to follow this  
dave

Which Is Faster In Assembler, Switch-Case or If-Elseif

This topic is 5046 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
Only one way to find out!

If I had to guess, I'd say switch is usually faster because the expression in the switch(x) only needs to be evaluated once. Whereas with several if statements x may need to be evaluated once for each if statement because the expressions may be completely different.

Share this post


Link to post
Share on other sites
As there's no "switch" command in assembly, the compiler has to compile it as if/else if/else's. Thus - it should be the same.

EDIT [0]: I'll give you an example [smile]

I usually code my switch statement in Window procedure (win32 assembly) as follows:


WndProc:
hWnd: equ 8
msg: equ 12
wParam: equ 16
lParam: equ 20

;...

;;switch (msg)
cmp dword [ebp + msg], WM_CREATE ;if (msg == WM_CREATE)
jmp .wm_create ; goto wm_create

cmp dword [ebp + msg], WM_DESTROY ;if (msg == WM_DESTROY)
jmp .wm_destroy ; goto wm_destroy

.wm_create:
;;stuff

.wm_destroy:
;;stuff





Oxyd

Share this post


Link to post
Share on other sites
In theory, modern compilers consider such series of conditions the same way. Depending on the constant arguments and the context of your code, they use 4 possible strategies (non exclusive) :

- reorder the ifs in a most favorable probabilistic manner.
- a binary tree.
- a look up table (case 1: case2: etc...)
- replace by conditional moves if possible

Else experiment choices and look at the asm output in your debugger or in a listing file. Maybe choosing switches guides the compiler more towards look up tables in some cases. Ifs more towards binary trees. Just my speculations.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
depends on your range of numbers for the switch.

optomized switchesuse jump tables indexed by the switch case values. so if your switch is say a INT and has spread wide
case values 0 1023 -5222 it probably will use if-then tests internally because the address table would be too big where a short range (max table size depends on compiler) ex- 0 1 2 3 4 5 6 7 8 etc... or even gaps 0 2 4 6 8 10 12 would have a table and jump to the indexed address in the table (may do a little arithmetic to convert to a 0 ofset for the lowest case value.

You compiler may have an ASM listing option that you can turn on and you can look at the code generated (using fairly rufimentary assembly code knowledge...)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
People please stop asking this question!!! Seacrh in the seacrh engine this is like the 2nd one of these in 2 weeks

Share this post


Link to post
Share on other sites
You won't be able to tell which one is faster, and they may compile down to the same code anyway. Nano-optimization sucks, too.

Share this post


Link to post
Share on other sites
A switch statement and an if-else-else if wouldn't necessarily compile out the same. A switch statement always compares against one expression that can be pre-calculated before any comparisons are made:



/* example one */
switch((x + 100) % 4)
{
case(0):
break;

case(1):
break;

case(2):
break;

default:
break;
}

/* example two */
if(((x + 100) % 4) == 0)
printf("zero\n");
else if(((x + 100) % 4) == 1)
printf("one\n");
else if(((x + 100) % 4) == 2)
printf("two\n");
else
printf("three\n");

/* example three */
if(x - 10 == 100)
printf("one hundred\n");
else if(x + 100 == 10)
printf("ten\n");




Examples one and two could potentially generate the same code. The left hand expression ((x + 100) % 4) doesn't need to be re-evaluated for each if statement, so it would work like a switch statement.

In example three the expression needs to be re-evaluated for each if statement. This would compile differently than a switch statement.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!