Main Content

本页的翻译已过时。点击此处可查看最新英文版本。

执行速度

提高生成的 C/C++ 代码的执行速度

利用代码生成选项和优化来提高所生成代码的执行速度。您可以修改或禁用可能会影响执行速度的动态内存分配。可以使用 parfor 循环生成并行代码。如果可用,可以利用预先存在的经过优化的 C 代码和专用库来加快执行速度。

有关如何针对特定条件优化代码的详细信息,请参阅优化策略

函数

parfor并行 for 循环
coder.varsize声明可变大小数据
coder.constFold expressions into constants in generated code
coder.inlineControl inlining of a specific function in generated code
coder.loop.parallelizeDisable automatic parallelization of a for loop
coder.unrollUnroll for-loop by making a copy of the loop body for each loop iteration
coder.ceval调用外部 C/C++ 函数

coder.LAPACKCallbackAbstract class for specifying the LAPACK library and LAPACKE header file for LAPACK calls in generated code
coder.BLASCallbackAbstract class for specifying the BLAS library and CBLAS header and data type information for BLAS calls in generated code
coder.fftw.StandaloneFFTW3Interface Abstract class for specifying an FFTW library for FFTW calls in generated code

示例和操作指南

可变大小数组

Minimize Dynamic Memory Allocation

Improve execution time by minimizing dynamic memory allocation.

Provide Maximum Size for Variable-Size Arrays

Use techniques to help the code generator determine the upper bound for a variable-size array.

Disable Dynamic Memory Allocation During Code Generation

Disable dynamic memory allocation in the app or at the command line.

Set Dynamic Memory Allocation Threshold

Disable dynamic memory allocation for arrays less than a certain size.

循环

使用并行 for 循环 (parfor) 生成代码

生成在共享内存多核平台上并行运行的循环。

Specify Maximum Number of Threads in parfor-Loops

Generate a MEX function that executes loop iterations in parallel on specific number of available cores.

Control Compilation of parfor-Loops

Treat parfor-loops as parfor-loops that run on a single thread.

Install OpenMP Library on macOS Platform

Install OpenMP library to generate parallel for-loops on macOS platform.

Minimize Redundant Operations in Loops

Move operations outside of loop when possible.

Unroll for-Loops and parfor-Loops

Control loop unrolling.

Automatically Parallelize for Loops in Generated Code

Iterations of parallel for loops can run simultaneously on multiple cores on the target hardware.

函数调用

Avoid Data Copies of Function Inputs in Generated Code

Generate code that passes input arguments by reference.

Control Inlining to Fine-Tune Performance and Readability of Generated Code

Inlining eliminates the overhead of function calls but can produce larger C/C++ code and reduce code readability.

Fold Function Calls into Constants

Reduce execution time by replacing expression with constant in the generated code.

数值边情形

Disable Support for Integer Overflow or Nonfinites

Improve performance by suppressing generation of supporting code to handle integer overflow or nonfinites.

外部代码集成

Integrate External/Custom Code

Improve performance by integrating your own optimized code.

Speed Up Linear Algebra in Generated Standalone Code by Using LAPACK Calls

Generate LAPACK calls for certain linear algebra functions. Specify LAPACK library to use.

Speed Up Matrix Operations in Generated Standalone Code by Using BLAS Calls

Generate BLAS calls for certain low-level matrix operations. Specify BLAS library to use.

Speed Up Fast Fourier Transforms in Generated Standalone Code by Using FFTW Library Calls

Generate FFTW library calls for fast Fourier transforms. Specify the FFTW library.

Synchronize Multithreaded Access to FFTW Planning in Generated Standalone Code

Implement FFT library callback class methods and provide supporting C code to prevent concurrent access to FFTW planning.

概念

优化策略

优化所生成的代码的执行速度或内存使用量。

Dynamic Memory Allocation and Performance

Dynamic memory allocation can slow down execution speeds.

使用并行 for 循环 (parfor) 的算法加速

parfor 循环生成 MEX 函数。

Classification of Variables in parfor-Loops

Variables inside parfor-loops are classified as loop, sliced, broadcast, reduction, or temporary.

Reduction Assignments in parfor-Loops

A reduction variable accumulates a value that depends on all the loop iterations together.

MATLAB Coder 对生成代码进行优化

代码生成器使用优化来提高生成代码的性能。

memcpy Optimization

The code generator optimizes generated code by using memcpy.

memset Optimization

The code generator optimizes generated code by using memset.

LAPACK Calls in Generated Code

LAPACK function calls improve the execution speed of code generated for certain linear algebra functions.

BLAS Calls in Generated Code

BLAS function calls improve the execution speed of code generated for certain low-level vector and matrix operations.

Generate Code That Uses Row-Major Array Layout

Generate C/C++ code with row elements stored contiguously in memory.

疑难解答

Troubleshooting parfor-Loops

Diagnose errors for code generation of parfor-loops.

MEX Generated on macOS Platform Stays Loaded in Memory

Troubleshoot issues that occur when the source MATLAB® code contains global or persistent variables that are reachable from the body of a parfor-loop.

Resolve Issue: Array or Variable Access Pattern Not Suitable for Parallel Execution

Troubleshoot automatic parallelization failure caused by memory access pattern inside a for loop.

特色示例