Main Content

Best Practices for Using MEX Functions to Accelerate MATLAB Algorithms

When you choose a section of MATLAB® code to accelerate, the following practices are recommended.

Accelerate Code That Dominates Execution Time

Find the section of MATLAB code that dominates run time. Accelerate this section of the code using a MEX function as follows:

  1. Place this section of the code inside a separate MATLAB function.

  2. From this MATLAB function, generate a MEX function.

  3. 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 profile viewer.

  • 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.

Include Loops Inside MEX Function

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, mat. 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
     % Section 3 ends
   % Section 2 ends

% 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.

Avoid Generating MEX Functions from Unsupported Functions

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 coder.extrinsic or wrapping it in an feval statement. See Use MATLAB Engine to Execute a Function Call in Generated Code.

Avoid Generating MEX Functions if Built-In MATLAB Functions Dominate Run Time

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 svd, eig ,fft, qr, lu.


You can invoke computationally intensive, built-in MATLAB functions from your MEX function. Declare the MATLAB function as extrinsic using coder.extrinsic or wrap it in an feval statement. For more information, see Use MATLAB Engine to Execute a Function Call in Generated Code.

Minimize MEX Function Calls

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,testfunc,which calls two functions,testfunc_1 and testfunc_2:

function [y1,y2] = testfunc(x1,x2)
  y1 = testfunc_1(x1,x2);
  y2 = testfunc_2(x1,x2);

Instead of generating MEX functions individually for testfunc_1 and testfunc_2, and then calling the MEX functions in testfunc, generate a MEX function for testfunc itself.