When you choose a section of MATLAB® code to accelerate, the following practices are recommended.
Find the section of MATLAB code that dominates run time. Accelerate this section of the code using a MEX function as follows:
Place this section of the code inside a separate MATLAB function.
From this MATLAB function, generate a MEX function.
From your original MATLAB code, call the MEX function.
To find the execution time of each MATLAB instruction, use MATLAB Profiler.
To open the Profiler from the command line, type
To open Profiler from the MATLAB Editor window, under the Editor tab, click Run and Time.
For more information about using the Profiler to measure run time of MATLAB code, see Profile Your Code to Improve Performance.
Instead of calling a MEX function inside a loop in the MATLAB code, include the loop inside the MEX function. Including the loop eliminates the overheads in calling the MEX function for every run of the loop.
For example, the following code finds the greatest element
in every row of a 1000–by–1000 matrix,
You can accelerate sections 1,2, and 3 using a MEX function.:
% Section 1 begins for i = 1:10000 % Section 2 begins max = mat(i,0); % Initialize max for j = 1:10000 % Section 3 begins if (mat(i,j) > max) max = mat(i,j) % Store the current maximum end % Section 3 ends end % Section 2 ends end % Section 1 ends
Accelerate section 1 using a MEX function. Accelerate section 1 first so that the MEX function is called only once.. If you cannot accelerate section 1 first, then accelerate sections 2 or 3, in that order. If section 2 (or 3) is accelerated using a MEX function, the function is called 10000 (or 10000 × 10000) times.
Check that the section of MATLAB code that you accelerate does not contain many functions and language features that are unsupported by MATLAB Coder™. For a list of supported functions, see Functions and Objects Supported for C/C++ Code Generation.
In certain situations, you might have to accelerate sections
of code even though they contain a few unsupported functions. Declare
an unsupported function as extrinsic to invoke the original MATLAB function
instead of the code generated for the function. You can declare a
function as extrinsic by using
wrapping it in an
See Extrinsic Functions.
Use MEX functions to accelerate MATLAB code only if user-generated code dominates the run time.
Avoid generating MEX functions if computationally intensive,
built-in MATLAB functions dominate the run time. These functions
are pre-compiled and optimized, so the MATLAB code is not accelerated
significantly using a MEX function. Examples of such functions include
Accelerate as much of the MATLAB code as possible using one MEX function instead of several MEX functions called at lower levels. This minimizes the overheads in calling the MEX functions.
For example, consider the function,
calls two functions,
function [y1,y2] = testfunc(x1,x2) y1 = testfunc_1(x1,x2); y2 = testfunc_2(x1,x2); end
Instead of generating MEX functions individually for
and then calling the MEX functions in
generate a MEX function for