Code optimization; switching against if

My question is whether to use “case” or “ifs” in a function that is called quite a lot. Here is the following, as of 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: will the switch work better? I know that if I use "ifs", I can place the most likely options at the top.

+5
source share
7 answers

You cannot use statements switchfor strings because they are pointers and are not checked at compile time. You are stuck using a set of instructions if.

, , , , .

, :

switch (value) {
  case frequent_value1:
  case frequent_value2:
  case frequent_value3:
    break;

default:
  switch (value) {
     case infrequent_value1:
     case infrequent_value2:
     case infrequent_value3:
        break;
     }
}
+10

gperf, "", . switch.

- :

switch (textbuff[0])
{
    case 'i':
    {
        switch (textbuff[1])
        {
            case 'p':
            {
                 switch (textbuff[2])
                 {
                     case 'a': /* something. */ break;
                     case 'r': /* something else. */ break;
                 }
            }
        }
    }

( ).

( 4 ), 32- , :

int32_t mashed =
    textbuff[0] << 24 | 
    textbuff[1] << 16 |
    textbuff[2] << 8 |
    textbuff[3];

switch (mashed) { /* ... */ }

, , , .

: ; ( ).

+5

std:: map.

class MessageMap
{ 
    std::map<std::string,int>    data;
    public:
        MessageMap()
        {
             data["text"]   = 1;
             data["name"]   = 2;
             data["list"]   = 3;
             data["remv"]   = 4;
             data["ipad"]   = 5;
             data["iprm"]   = 6;
        }
        int getMessageId(std::string const& index) cosnt
        {
            std::map<std::string,int>::const_iterator f;
            if ((f = data.find(index)) != data.end())
            {
                return f->second;
            }
            return 0;
        }
};
int identifyMsg(char* textbuff)
{
    static MessageMap   mssageMap;
    return messageMap.getMessageId(textbuff);
}
+4

"" . case switch.

+2

, , , - :

int identifyMsg(const char* textbuff) {
    static const struct { const char* str; int id; } pairs[] = {
        { "text", 1 },
        { "name", 2 },
        { "list", 3 },
        { "remv", 4 },
        { "ipad", 5 },
        { "iprm", 6 },
    };
    for (int i = 0; i < sizeof(pairs)/sizeof(pairs[0]); ++i) {
        if (!strcmp(textbuff, pairs[i].str))
            return pairs[i].id;
    }
    return 0;
}
+2

, . if/switch.

, - . , . . , , . ? IPC, XML, ? ? ? , , .

, Purify, gperf, cachegrind?

, /switch: switch . (char, short, int, long, enum)

+2

, :

strcmp , : 4 , 32- - 32- , .

-:

  • -.

  • , . , strcmp , .

As a side note, compilers such as MSVC and GCC have implemented switch-based optimization that checks for switching conditions using binary search. Thus, a switch statement with, say, 256 elements will be optimized for up to 8 comparison operations.

+2
source

All Articles