Programming and Data Types    

What MATLAB Does Not Accelerate

The following programming elements and practices can slow MATLAB down and should be avoided when speed is the primary consideration.

Data Types and Array Shapes

Data types not supported for acceleration are shown as unshaded in the figure in the previous section. The only unaccelerated array shapes are arrays having more than three dimensions.

Function Calls

Calls to other functions (M-file or MEX) or subfunctions are costly in terms of performance and are not accelerated in MATLAB. The code that makes up the function being called may have improved performance, but the mechanism of making the call may reduce this benefit.

Functions That Overload MATLAB Built-Ins.   Since the execution of an overloaded function involves making a function call to an M-file or MEX-file, overloading a MATLAB built-in function slows down its execution. For example, if you overload the eq built-in function for type char, then comparing characters for equality will run slower than when using the MATLAB built-in eq, which is accelerated for characters.

More than One Operation on a Line

Under certain circumstances, including more than one operation on a line in your program might slow it down. In the example shown here, the first operation involves a structure array, which is not supported by the performance accelerator.

Since MATLAB processes the code sequentially, an entire line at a time, any instructions that follow the disqualifying operation on the same line are not accelerated. This means that the for loop in this example would not get the performance enhancement it would have gotten had it been on a separate line.

Changing Data Types or Shapes of Variables

If your program changes the data type or array shape of an existing variable, MATLAB must temporarily stop its code acceleration process and handle the change. For example, this code changes the type for X from double to char, and thus slows the acceleration process.

Using Complex Constants in Scripts

In scripts, by default MATLAB interprets the terms i and j to be variables rather than the MATLAB complex constants i and j. If you use i and j as complex constants in scripts, you need to make it clear in the script that they are to be interpreted as such. If it is not clear, then MATLAB will treat them as undefined variables and, as a result, will not accelerate the statements in which they appear.

For example, MATLAB interprets i in the following statement to be a variable of undefined data type and array size. As such, MATLAB must temporarily stop the acceleration process to interpret the statement.

This next statement expresses the same equation in a way that cannot be misinterpreted. The phrase 2i only makes sense if i represents the imaginary constant. Expressed in this way, the statement does accelerate.

The same holds true for a simple statement such as x = i. If you express this as x = 1i, MATLAB accelerates the statement.


  Performance Acceleration How Vectorizing and Preallocation Fit In