Main Content

Reduce Code Generation Time

R2026b

When developing MATLAB® code for code generation iteratively, long code generation times can accumulate. Depending on your project and code generation goals, you can reduce the amount of time that it takes to generate code by omitting compilation or code evaluation, or by disabling the code generation report. This table lists the approaches that can reduce code generation time:

GoalAction
You generate MEX functions frequently for iterative development and code generation.

Use Just-In-Time Compilation to Generate MEX Functions

You want to inspect the generated C/C++ code without waiting for compilation to finish.

Generate Only Code Without Compiling

You do not need to generate a code generation report.

Disable Code Generation Report

You fold computationally expensive expressions to constants in the generated code.

Use Extrinsic Evaluation to Reduce Constant-Folding Time

Use Just-In-Time Compilation to Generate MEX Functions

By default, when you generate a MEX function, the code generator produces C or C++ source code and compiles the source code into a MEX binary. Just-in-time (JIT) compilation produces a JIT MEX function that contains an abstract representation of the MATLAB code. Because JIT compilation does not generate and then compile the C code, producing the MEX is faster. At run time, MATLAB generates the executable code from the JIT MEX function in memory.

To enable JIT compilation, use one of these approaches:

You cannot use JIT compilation if:

  • The code uses coder.ceval to integrate custom code.

  • The code uses coder.updateBuildInfo to update build information.

  • You want to generate C or C++ source code only.

  • You use the OpenMP application interface to parallelize for-loops in the MATLAB code using parfor.

Note

JIT MEX functions are not compatible across different releases of MATLAB Coder™. Run the JIT MEX function using the same release that generated it.

Generate Only Code Without Compiling

If you generate code iteratively and your iteration focuses on the structure of the generated code rather than the compiled output, you can inspect the code without compiling it. Skipping the compilation step speeds up the iterative development.

To generate code without compiling, use one of these approaches:

  • At the command line, use codegen with the option -c.

  • In a code configuration object, set the GenCodeOnly property to true.

  • In the Code Generation Settings dialog box, select the Generate code only check box.

Because this method skips compilation, , you cannot run the generated code until you perform a full build. For details about configuring a full build, see Configure Code Generation Settings.

Disable Code Generation Report

The code generation report is an HTML summary of the generated code, including type information, code insights, and traceability. If you do not need the report, you can disable report creation.

Disable report creation by using one of these approaches:

  • At the command line, omit the codegen options -report or -launchreport .

  • In a code configuration object, set the GenerateReport property to false.

  • In the Code Generation Settings dialog box, clear the Always create a report checkbox.

Note

If an error occurs during code generation, MATLAB Coder creates a report even if you disable this setting.

Use Extrinsic Evaluation to Reduce Constant-Folding Time

When you use coder.const to fold a function call into a constant in the generated code, the code generator evaluates the expression during code generation. If that expression is computationally intensive, code generation slows down. You can reduce this overhead by making the function call extrinsic, which forces the code generator to evaluate the coder.const function call in MATLAB at run time.

To designate a function as extrinsic, use coder.extrinsic or feval. For example, this code specifies the functions besselj and readmatrix as extrinsic for the entire forceConstFold function.

function out = forceConstFold(in) %#codegen
coder.extrinsic("besselj","readmatrix");
zTable = coder.const(readmatrix("mytable.txt"));
jTable = coder.const(besselj(3,zTable));
out = interp1(zTable,jTable,in);
end

Because zTable and jTable are both coder.const objects, the code generator evaluates these objects at code generation time. Because the functions besselj and readmatrix are extrinsic, MATLAB does the expensive evaluation, which results in faster code generation.

When you declare a function extrinsic by using coder.extrinsic, the code generator treats all calls to that function within the current function scope as extrinsic. To specify that a single function call is extrinsic, use feval.

See Also

| | | | |

Topics