Main Content

Increase Code Generation Speed

The amount of time it takes to generate code for a model depends on the size and configuration settings of the model. For instance, if you are working with a large model, it can take awhile to generate code. To decrease the amount of time for code generation of a model, try one or more of the following methods:

  • Build a model in increments

  • Build large model reference hierarchies in parallel

  • Minimize memory requirements during code generation

  • Generate only code

  • Disable the creation of a code generation report

Build a Model in Increments

You can use the slbuild command to build a model and generate code. By default, when rebuilding a model, slbuild provides an incremental model build, which only rebuilds a model or submodels that have changed since the most recent model build. Incremental model build saves code generation time. Use the Rebuild parameter on the Model Referencing pane to change the method that Simulink® uses to determine when to rebuild code for referenced models. For more information on the Rebuild parameter, see Rebuild.

Build Large Model Reference Hierarchies in Parallel

In a parallel computing environment, whenever conditions allow, you can increase the speed of code generation and compilation by building models containing large model reference hierarchies in parallel. For example, if you have Parallel Computing Toolbox™ software, you can distribute code generation and compilation for each referenced model across the cores of a multicore host computer. If you have MATLAB® Parallel Server™ software, you can distribute code generation and compilation for each referenced model across remote workers in your MATLAB Parallel Server configuration.

The execution speed improvement realized by using parallel builds for referenced models depends on several factors, including:

  • How many models can be built in parallel for a given model referencing hierarchy

  • The size of the referenced models

  • Parallel computing resources such as the number of local and remote workers available

  • The hardware attributes of the local and remote machines (amount of RAM, number of cores, and so on)

For more information, see Reduce Build Time for Referenced Models by Using Parallel Builds.

Minimize Memory Requirements During Code Generation

Models that have large amounts of parameter and constant data (such as lookup tables) can tax memory resources and slow code generation. The code generator copies this data to the model.rtw file. The model.rtw file is a partial representation of the model that the Target Language Compiler parses to transform block computations, parameters, signals, and constant data into a high-level language (for example, C). The Target Language Compiler (TLC) is an integral part of the code generator. The code generator copies parameters and data into model.rtw, whether they originate in the model or come from variables or objects in a workspace.

You can improve code generation speed by specifying the maximum number of elements that data vectors can have for the code generator to copy this data to model.rtw. When a data vector exceeds the specified size, the code generator places a reference key in model.rtw. The TLC uses this key to access the data from Simulink and format it into the generated code. Reference keys result in maintaining only one copy of large data vectors in memory.

The default value above which the code generator uses reference keys in place of actual data values is 10 elements. You can verify this value. In the Command Window, type the following command:

get_param(0, 'RTWDataReferencesMinSize')

To set the threshold to a different value, in the Command Window, type the following set_param function:

set_param(0, 'RTWDataReferencesMinSize', <size>)

Provide an integer value for size that specifies the number of data elements above which the code generator uses reference keys in place of actual data values.

Generate Only Code

You can increase code generation speed by specifying that the build process generate code and a makefile, but not invoke the make command. When the code generator invokes the make command, the build process takes longer because the code generator generates code, compiles code, and creates an executable file.

On the Code Generation pane in the Model Configuration Parameters dialog box, you can specify that the build process generate only code by selecting the Generate code only parameter. You can specify that the code generation process build a makefile by selecting the Configuration Parameters > Code Generation > Build process > Makefile configuration > Generate makefile parameter.

Suppress Creation of a Code Generation Report

You can speed up code generation by not generating a code generation report as a part of the build process. To disable the creation of a code generation report, on the Code Generation > Report pane, clear the Create code generation report parameter. To generate a code generation report after the build process, see Generate Code Generation Report (Embedded Coder).

Related Topics