|Programming and Data Types|
The MathWorks is committed to making MATLAB as fast as 3GL programming languages, like C and Fortran. Speeding up the execution of programs written in MATLAB is an ongoing endeavor that will be delivered over a number of product releases.
This section discusses MATLAB performance acceleration, a capability introduced in MATLAB 6.5 that is aimed at speeding up the processing of M-file functions and scripts. As acceleration is a work in progress, its effect is greater on some components of the MATLAB language than on others at this time. For some M-file programs, you will see a marked improvement, and on others, a lesser, if any, effect.
In MATLAB 6.5, the most significant performance improvement is in functions and scripts that spend most of their time in self-contained loops, particularly loops that make no calls to M-file functions. The accelerated loops often execute at least as fast as a vectorized version of the same loop. See Sample Accelerated Programs for examples of such programs and comparative performance measurements.
The following sections explain how to write programs in MATLAB to maximize the benefit of performance acceleration. The first two sections, in particular, discuss which programming constructs to use and which to avoid to achieve optimal performance:
|Note The MATLAB Profiler has been enhanced to help you measure and optimize the performance benefits covered here. See Measuring Performance for more information.|
What MATLAB Accelerates
MATLAB performance enhancements are supported on a subset of the entire MATLAB language capability. This section defines the supported subset of data types, array shapes, and operations that execute faster.
Your programs will see the greatest speed improvement when large, contiguous portions of your code are restricted to only those elements of MATLAB that support performance acceleration. Whenever MATLAB encounters an unsupported element, (for example, one of the unaccelerated data types in the diagram below), it interrupts accelerated processing to handle the instruction through the non-accelerated interpreter. The more MATLAB has to do this, the greater price you pay in execution time.
MATLAB accelerates code that uses the data types that are shaded in the class hierarchy diagram below, and does not accelerate those that are unshaded. Both real and complex
doubles are accelerated. Only full arrays, not sparse, are accelerated.
Performance acceleration applies to all MATLAB array shapes except for arrays having more than three dimensions.
Loops controlled by a
for statement execute faster in MATLAB as long as:
forloop are set to a range of scalar values.
forloop uses only the supported data types and array shapes.
forloop calls any functions, they are built-in functions.
Loop performance is optimal when every line of code in the loop meets the requirements for acceleration. When this is the case, MATLAB speeds up execution of the entire loop, including the
end statements. If this is not true, then acceleration of the loop is temporarily interrupted on each iteration of the loop to process the disqualifying lines, as well as the
Read the section on What MATLAB Does Not Accelerate to see what practices you should avoid using in your
for loop code.
switch statements execute faster as long as the conditional expression in such statements evaluates to a scalar value.
There is a certain amount of overhead work that MATLAB performs when manipulating arrays. For large arrays, this overhead represents only a small percentage of the time required to handle the data. For small matrices, however, the overhead is a greater percentage of the time spent and thus is more significant.
Performance acceleration reduces the amount of overhead required to handle arrays. You will see more of a speed up in programs that use smaller arrays.
|Other Ways to Speed Up Performance||What MATLAB Does Not Accelerate|