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

Started by
19 comments, last by Spoonbender 19 years, 6 months ago
Do they both compile to the same assembler? ace
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.
I cant be sure (never checked) but Id assume that the compiler would optimize them out to the same thing?
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
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.
"Coding math tricks in asm is more fun than Java"
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...)
People please stop asking this question!!! Seacrh in the seacrh engine this is like the 2nd one of these in 2 weeks
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.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
I'd ReplaceConditionalWithPolymorphism. You'll almost certainly end up with a lookup table then.
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.

This topic is closed to new replies.

Advertisement