On April 1, 2008 03:02:02 am Robert Staudinger wrote:
> On Sat, Mar 29, 2008 at 9:21 PM, Jose Da Silva <digital@joescat.com>
> wrote:
>
> [...]
>
> > In summary, the switch statement looks nicer from a source-code point
> > of view, but the if statement is a bit less code in a compiled sense.
>
> Unless you have data suggesting that it makes a big difference, the
> nicer source code should be preferred.
past experience with some other "older" compilers or cross compilers had
issues with creating tables of empty data if the values were far apart and
if you had a lot of switch statements, the amount of space would begin to
add up.
I tried gcc v4.2 and it appears it already does the conversion so therefore
not an issue with gcc v4.2 at least. Data in this example agrees with you.
-------------------------
#include <stdio.h>
int main(int argc, const char *argv[]) {
int i;
for (i = 0; i < 10; i++)
switch (i) {
case 3: putchar('a'); break;
case 7: putchar('\n'); break;
default: putchar('.'); break;
}
// if (i == 3) putchar('a');
// else if (i == 7) putchar('\n');
// else putchar('.');
return(0);
}
-------------------------
gcc -S -o file.asm file.c
shows this in the resulting output file
cmpl $3, %eax
jne .L13
etc.........
.L13:
cmpl $7, %eax
je .L11
or in other words, the switch example above is converted internally by gcc
to 2 if statements instead of using tables.
Received on Wed Apr 2 10:50:15 2008
This archive was generated by hypermail 2.1.8 : Wed Apr 02 2008 - 10:50:15 CEST