I mainly program microcontrollers in C, but I also develop windows software in C#. Yesterday, while writing code for an SMS system in C, I encountered a situation that has happened to me before, but this time I think I’m obsessed with it! My main concern is the performance of the switch statement and if-else. Which statement performs better in C? Visually, I prefer the switch, and it’s easier to read, but in this specific case, I’m wondering whether the switch or if-else is better in terms of performance.
The purpose of the code is to check if an SMS needs to be sent. For that, I have a 16-bit variable, where each bit represents a specific action to be performed:
MESSAGE_TYPE TypeMsgToSend_ui16 = MESSAGE_VOID;
typedef enum _MESSAGE_TYPE
{
MESSAGE_VOID = (uint16_t)0x00,
MESSAGE_NO_SIGNAL_1 = (uint16_t)0x01,
MESSAGE_WITH_SIGNAL_1 = (uint16_t)0x02,
MESSAGE_NO_SIGNAL_2 = (uint16_t)0x04,
MESSAGE_WITH_SIGNAL_2 = (uint16_t)0x08,
MESSAGE_NO_SIGNAL_3 = (uint16_t)0x10,
MESSAGE_WITH_SIGNAL_3 = (uint16_t)0x20,
MESSAGE_NO_SIGNAL_4 = (uint16_t)0x40,
MESSAGE_WITH_SIGNAL_4 = (uint16_t)0x80,
MESSAGE_RUT_RESET = (uint16_t)0x100,
MESSAGE_TEST_MESSAGE = (uint16_t)0x200,
MESSAGE_RESET_CONFIRMATION = (uint16_t)0x400,
MESSAGE_RESERVED_1 = (uint16_t)0x800,
MESSAGE_RESERVED_2 = (uint16_t)0x1000,
MESSAGE_RESERVED_3 = (uint16_t)0x2000,
MESSAGE_RESERVED_4 = (uint16_t)0x4000,
MESSAGE_RESERVED_5 = (uint16_t)0x8000,
} MESSAGE_TYPE;
Currently, I have implemented an algorithm using if-else:
if (TypeMsgToSend_ui16 != MESSAGE_VOID) {
if ((TypeMsgToSend_ui16 & MESSAGE_TEST_MESSAGE) == MESSAGE_TEST_MESSAGE) {
TypeMsgToSend_ui16 &= ~(MESSAGE_TEST_MESSAGE);
Encode_Command(RUT_COMMAND_TO_EXECUTE_SMS_SEND, PHONENUMBER, TEST_MESSAGE_TEXT);
}
else {
if ((TypeMsgToSend_ui16 & MESSAGE_RESET_CONFIRMATION) == MESSAGE_RESET_CONFIRMATION) {
TypeMsgToSend_ui16 &= ~(MESSAGE_RESET_CONFIRMATION);
Encode_Command(RUT_COMMAND_TO_EXECUTE_SMS_SEND, PHONENUMBER, RESET_CONFIRMATION_TEXT);
}
else {
// ...
}
}
}
The order of checks determines which SMS will be sent. My question is whether this method is more or less efficient than using a for loop to go through the bits of TypeMsgToSend_ui16 with a switch case inside the loop.