We have a method for writing command data to a device. The method first converts the data to the form accepted by the device and then writes the data to the serial port. The data conversion is done with a case statement as given below. For 10 commands we need to convert data. For other commands,we don't have to convert the data (around 10 commands).
Customer complaints that code is not optimized. Some of the commands for which data conversion is not required are frequently used.
Will direct if else statements optimize code?
Is there any other options to optimize the code in this case?
switch (cmd_no)
{
case CMD_WR_ACC:
converted_command_data = (INT32)((((DOUBLE)cmd_data * CMD_WR_ACC_PARA1) / CMD_WR_ACC_PARA2) + 0.5);
break;
case CMD_WR_BIAS:
converted_command_data = (INT32)((((DOUBLE)cmd_data * CMD_WR_BIAS_PARA1) / CMD_WR_BIAS_PARA2) + 0.5);
break;
case CMD_WR_SUP:
converted_command_data = (INT32)((((DOUBLE)cmd_data * CMD_WR_SUP_PARA1) / CMD_WR_SUP_PARA2) + 0.5);
break;
case CMD_WR_FIL:
converted_command_data = (INT32)((((DOUBLE)cmd_data * CMD_WR_FIL_PARA1) / CMD_WR_FIL_PARA2) + 0.5);
break;
.
.
default:
converted_command_data = cmd_data;
break;
}