Programming and Data Types    

Optimizing for Speed

This section covers the following topics:

For more information: See Maximizing MATLAB Performance in the MATLAB "Programming and Data Types" documentation

Finding Bottlenecks with the Profiler

A good first step to speeding up your programs is to use the MATLAB Profiler to find out where the bottlenecks are. This is where you need to concentrate your attention to optimize your code.

To start the Profiler, type profile viewer or select View -> Profiler in the MATLAB desktop.

For more information: See Measuring Performance in the MATLAB "Programming and Data Types" documentation, and the profile function reference page

Measuring Execution Time with tic and toc

The functions tic and toc help you to measure the execution time of a piece of code. You may want to test different algorithms to see how they compare in execution time.

Use tic and toc as shown here.

For more information: See Techniques for Improving Performance in the MATLAB "Programming and Data Types" documentation, and the tic/toc function reference page

Measuring Smaller Programs

Programs can sometimes run too fast to get useful data from tic and toc. When this is the case, try measuring the program running repeatedly in a loop, and then average to find the time for a single run.

Speeding Up MATLAB Performance

MATLAB internally processes much of the code in M-file functions and scripts to run at an accelerated speed. The effects of performance acceleration can be particularly noticeable when you use for loops and, in some cases, the accelerated loops run as fast as vectorized code.

Implementing performance acceleration in MATLAB is a work in progress, and not all components of the MATLAB language can be accelerated at this time. Read Performance Acceleration in the MATLAB "Programming and Data Types" documentation to see how you can make the best use of this feature.

Vectorizing Your Code

It's best to avoid the use of for loops in programs that cannot benefit from performance acceleration. Unlike compiled languages, MATLAB interprets each line in a for loop on each iteration of the loop. Most loops can be eliminated by performing an equivalent operation using MATLAB vectors instead. In many cases, this is fairly easy to do and is well worth the effort required to convert from using a loop.

For more information: See Vectorizing Loops in the MATLAB "Programming and Data Types" documentation

Functions Used in Vectorizing

Some of the most commonly used functions for vectorizing are

all
end
logical
repmat
squeeze
any
find
ndgrid
reshape
sub2ind
cumsum
ind2sub
permute
shiftdim
sum
diff
ipermute
prod
sort

Coding Loops in a MEX-File for Speed

If there are instances where you must use a for loop, consider coding the loop in a MEX-file. In this way, the loop executes much more quickly since the instructions in the loop do not have to be interpreted each time they execute.

For more information: See Introducing MEX-Files in the External Interfaces/API documentation

Preallocate to Improve Performance

MATLAB allows you to increase the size of an existing matrix incrementally, usually within a for or while loop. However, this can slow a program down considerably, as MATLAB must continually allocate more memory for the growing matrix and also move data in memory whenever a contiguous block cannot be allocated.

It is much faster to preallocate a block of memory large enough to hold the matrix at its final size. For example, to preallocate a 10000-by-10000 matrix, use

For more information: See Preallocating Arrays in the MATLAB "Programming and Data Types" documentation

Functions Are Faster Than Scripts

Your code executes more quickly if it is implemented in a function rather than a script. Every time a script is used in MATLAB, it is loaded into memory and evaluated one line at a time. Functions, on the other hand, are compiled into pseudo-code and loaded into memory once. Therefore, additional calls to the function are faster.

For more information: See Techniques for Improving Performance in the MATLAB "Programming and Data Types" documentation

Avoid Large Background Processes

Avoid running large processes in the background at the same time you are executing your program in MATLAB. This frees more CPU time for your MATLAB session.

Load and Save Are Faster Than File I/O Functions

If you have a choice of whether to use load and save instead of the MATLAB file I/O routines, choose the former. load and save have been optimized to run faster and reduce memory fragmentation.

Conserving Both Time and Memory

The following tips have already been mentioned under Managing Memory, but apply to optimizing for speed as well:


  Managing Memory Starting MATLAB