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 thertwbuildcommand to build a model and generate code. By default, when rebuilding a model,rtwbuildprovides 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 theRebuildparameter on theModel Referencingpane to change the method that Simulink®uses to determine when to rebuild code for referenced models. For more information on theRebuildparameter, seeRebuild.

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 haveMATLAB®Parallel Server™software, you can distribute code generation and compilation for each referenced model across remote workers in yourMATLAB Parallel Serverconfiguration.

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, seeReduce Build Time for Referenced Models by Using Parallel Builds.

减少内存需求在代码9n

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 themodel.rtwfile. Themodel.rtwfile 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 intomodel.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 tomodel.rtw. When a data vector exceeds the specified size, the code generator places a reference key inmodel.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 followingset_paramfunction:

set_param(0,'RTWDataReferencesMinSize', )

Provide an integer value forsizethat 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 theCode Generationpane in the Model Configuration Parameters dialog box, you can specify that the build process generate only code by selecting theGenerate code onlyparameter. You can specify that the code generation process build a makefile by selecting theConfiguration Parameters>Code Generation>Build process>Makefile configuration>Generate makefileparameter.

Suppress Creation of a Code Generation Report

你不可以加快代码生成,生成a code generation report as a part of the build process. To disable the creation of a code generation report, on theCode Generation>Reportpane, clear theCreate code generation reportparameter. After the build process, you can generate a code generation report by doing this procedure,Generate Code Generation Report After Build Process.

Related Topics