code optimization; switch versus if's
Posted
by
KaiserJohaan
on Stack Overflow
See other posts from Stack Overflow
or by KaiserJohaan
Published on 2011-01-30T12:47:25Z
Indexed on
2011/01/30
15:25 UTC
Read the original article
Hit count: 296
Hello,
I have a question about whether to use 'case' or 'ifs' in a function that gets called quite alot. Here's the following as it is now, in 'ifs'; the code is self-explanatory:
int identifyMsg(char* textbuff) {
if (!strcmp(textbuff,"text")) {
return 1;
}
if (!strcmp(textbuff,"name")) {
return 2;
}
if (!strcmp(textbuff,"list")) {
return 3;
}
if (!strcmp(textbuff,"remv")) {
return 4;
}
if (!strcmp(textbuff,"ipad")) {
return 5;
}
if (!strcmp(textbuff,"iprm")) {
return 6;
}
return 0;
}
My question is: Would a switch perform better? I know if using ifs, I can place the most likely options at the top.
© Stack Overflow or respective owner