By profiling the execution speed of generated code, you can help verify that the code meets execution speed requirements.
Profiling can be especially important early in the development cycle for identifying potential architectural issues that can be more expensive to address later in the process. Profiling can also identify bottlenecks and procedural issues that indicate a need for optimization, for example, with an inner loop or inline code.
If you have an Embedded Coder® license, see Code Execution Profiling (Embedded Coder) for an alternative and simpler approach based on software-in-the-loop (SIL) or processor-in-the-loop (PIL) simulations.
You can profile code generated with code generation technology by using a Target Language Compiler (TLC) hook function interface.
To use the profile hook function interface:
For your system target file, create a TLC file that defines the following hook functions. Write the functions so that they specify profiling code. The code generator adds the hook function code to code generated for atomic systems in the model.
|Function||Input Arguments||Output Type||Description|
|Array of header file names||Return an array of the header file names to be included in the generated code.|
|Generate code statements for profiler type definitions.|
|Global data for the specified
||Generate code statements that declare global data.|
|Generate code statements that create global
|Declarations for the specified ||Generate code for required variable declarations within
the scope of an atomic subsystem
|Profiler start commands for the specified
||Generate code that starts the profiler within the scope
of an atomic subsystem |
|Profiler end commands for the specified
||Generate code that stops the profiler within the scope of
|Profiler termination code for the specified
||Generate code that terminates profiling (and possibly reports results) for an atomic subsystem.|
define the following global variables.
|The name of the TLC file that you created in step 1|
A quick way to define global variables is to define the parameters with
-a option. You can apply this option by using the
set_param command to set the model configuration
TLCOptions. For example,
>> set_param(gcs,'TLCOptions', ... '-aProfileGenCode=1 -aProfilerTLC="rtwdemo_profile_hook.tlc"')
Consider setting configuration parameters for generating a code generation report. You can then examine the profiling code in the context of the code generated for the model.
Build the model. The build process embeds the profiling code in the hook function locations in the generated code for the model.
Run the generated executable file. In the MATLAB Command Window, enter
!. You see
the profiling report you programmed in the profiling TLC file that you
created. For example, a profile report could list the number of calls made
to each system in a model and the number of CPU cycles spent in each
For details on programming a
.tlc file and defining TLC
configuration variables, see Target Language Compiler.
The TLC hook function interface for profiling code execution speed does not
support the S-function system target file (