Main Content

Optimize Generated Code by Consolidating Redundant If-Else Statements

This example shows how to optimize generated code by combining if-else statements that share the same condition. This optimization:

  • Improves control flow.

  • Reduces code size.

  • Reduces RAM consumption.

  • Increases execution speed.

Example

The model ControlFlowOptimization contains three Switch blocks. The Constant block provides the control input to the Switch blocks. The variable named Cond determines the value of the Constant block.

model = 'ControlFlowOptimization';
open_system(model);

Generate Code

Save the model in your current working directory. Build the model.

slbuild(model)
### Starting build procedure for: ControlFlowOptimization
### Successful completion of build procedure for: ControlFlowOptimization

Build Summary

Top model targets:

Model                    Build Reason                                         Status                        Build Duration
==========================================================================================================================
ControlFlowOptimization  Information cache folder or artifacts were missing.  Code generated and compiled.  0h 0m 13.185s 

1 of 1 models built (0 models already up to date)
Build duration: 0h 0m 15.058s

These lines of ControlFlowOptimization.c code show that in the generated code, two if-else statements and one else-if statement represent the three Switch blocks.

cfile = fullfile('ControlFlowOptimization_ert_rtw',...
    'ControlFlowOptimization.c');
coder.example.extractLines(cfile,'/* Model step', '/* Model initialize', 1, 0);
/* Model step function */
void ControlFlowOptimization_step(void)
{
  /* Switch: '<Root>/Switch3' incorporates:
   *  Constant: '<Root>/Const'
   *  Switch: '<Root>/Switch2'
   */
  if (Cond) {
    /* Switch: '<Root>/Switch1' */
    if (Cond) {
      /* Outport: '<Root>/Out1' incorporates:
       *  Inport: '<Root>/In1'
       */
      rtY.Out1 = rtU.In1;
    } else {
      /* Outport: '<Root>/Out1' incorporates:
       *  Inport: '<Root>/In2'
       */
      rtY.Out1 = rtU.In2;
    }

    /* End of Switch: '<Root>/Switch1' */
  } else if (Cond) {
    /* Switch: '<Root>/Switch2' incorporates:
     *  Inport: '<Root>/In1'
     *  Outport: '<Root>/Out1'
     */
    rtY.Out1 = rtU.In1;
  } else {
    /* Outport: '<Root>/Out1' incorporates:
     *  Inport: '<Root>/In2'
     */
    rtY.Out1 = rtU.In2;
  }

  /* End of Switch: '<Root>/Switch3' */
}

Enable Optimization

  1. Open the Configuration Parameters dialog box.

  2. On the Code generation-> Code Style pane, clear Preserve condition expression in if statement. This parameter is on by default.

Alternatively, use the command-line API to turn off the parameter:

set_param(model, 'PreserveIfCondition', 'off');

Generate Code with Optimization

In the optimized code, the code generator consolidates the two if-else statements and one else-if statement into one if-else statement. The code generator consolidates these statements because they all share the same condition. There is no intervening code that affects the outcomes of these statements.

Build the model.

slbuild(model)
### Starting build procedure for: ControlFlowOptimization
### Successful completion of build procedure for: ControlFlowOptimization

Build Summary

Top model targets:

Model                    Build Reason                     Status                        Build Duration
======================================================================================================
ControlFlowOptimization  Generated code was out of date.  Code generated and compiled.  0h 0m 11.008s 

1 of 1 models built (0 models already up to date)
Build duration: 0h 0m 12.013s

Here is the ControlFlowOptimization.c optimized code.

coder.example.extractLines(cfile,'/* Model step', '/* Model initialize', 1, 0);
/* Model step function */
void ControlFlowOptimization_step(void)
{
  /* Switch: '<Root>/Switch1' incorporates:
   *  Constant: '<Root>/Const'
   *  Switch: '<Root>/Switch3'
   */
  if (Cond) {
    /* Outport: '<Root>/Out1' incorporates:
     *  Inport: '<Root>/In1'
     */
    rtY.Out1 = rtU.In1;
  } else {
    /* Outport: '<Root>/Out1' incorporates:
     *  Inport: '<Root>/In2'
     */
    rtY.Out1 = rtU.In2;
  }

  /* End of Switch: '<Root>/Switch1' */
}

Close the model and clean up.

bdclose(model)

See Also

Related Topics