difference between switch, default [] operator and if else.

(no optimizers)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int arr[4] = {0,1,2,3}

int m1(int i){
    return arr[i]
}

int m2(int i){
    switch(i){
    case 0: return 0;
    case 1: return 1;
    ... you get the idea
}

int m3(int i){
    if(i == 0) return 0;
    else if(i == 1) return 1;
    ... you get the idea
}


m1,m2 and m3 have same functionality but have different mechanics.
m3 be worst, is there a difference between m1 and m2?

I don't know how they'll end up on assembly after compiled.
Switch iterators or element index don't have to be linear.
Array's if you want iterator values of arr[1] and arr[4]
arr[0], arr[2] and arr[3] will also be defined.

for switch
switch(i) { case 1: return 1; case 4: return 2; }
and you don't have to define other elements.

I heard a rumor that compiler makes other elements happen so it
can find things faster, so it would be linear.
so between case 1: and case 1000:, there are 999 empty cases.
I find that hard to believe.

How does switch (m2) operate faster than m3?
( if the iter values are not linear like in this example. )

How does switch really work?
Last edited on
> I don't know how they'll end up on assembly after compiled.
Just do gcc -S prog.c and find out by examining prog.s
Other compilers will have similar options.

> How does switch really work?
Like a chain of if / else if.
With some tweaks if there is no break statement at the end of a case.

A jump table is a possible implementation, but it's not the only one.
The standard only describes a behaviour.

> I heard a rumor that compiler makes other elements happen so it
> can find things faster, so it would be linear.
Well that really depends on the compiler.
There are all sorts of factors that might alter what gets generated.
- the machine architecture you're compiling for.
- what optimisation level.
- whether you're optimising for speed or memory space.
- whether you have run-time profile information about which cases are most likely.

Old, but still informative.
http://www.jauu.net/2010/06/15/gcc-generated-switch-jump-tables/
If you look at these three functions in Compiler Explorer (https://godbolt.org/z/ReXxgu) you'll see that GCC with -O2 generates equivalent code for both m1 and m2.
The nice thing about a switch statement is that it gives the compiler the opportunity to pick the most efficient implementation in each case. It might do the equivalent of if/else if/ else if/... (but evaluating the index just once). More likely, it will create a jump table, and then index into it directly, or do a linear search, or a binary search.

The bottom line is that the compiler writers know the most efficient implementation for each case. Unless you can use m1(), code it as a switch and let the compiler decide how best to implement it.
I heard a rumor that compiler makes other elements happen so it
can find things faster, so it would be linear.
so between case 1: and case 1000:, there are 999 empty cases.
I find that hard to believe.



1000, even 64 or so bit values, is still very tiny, so it might actually do that. Eventually, though, the size become big enough to trigger something and it will use another approach.
Topic archived. No new replies allowed.