MATLAB Guide

Most users of MATLAB find that computations are completed fast enough that execution time is not usually a cause for concern. Some computations, though, particularly when the problems are large, require a significant time and it is natural to ask whether anything can be done to speed them up. This chapter describes some techniques that produce better performance from M-files. They all exploit the fact that MATLAB is an interpreted language with dynamic memory allocation. Another approach to optimization is to compile rather than interpret MATLAB code. The MATLAB Compiler, available from The Math Works as a separate product, translates MATLAB code into C and compiles it with a C compiler. External C or Fortran codes can also be called from MATLAB via the MEX interface; see [54], [55].
Vectorization, discussed in the first section, has benefits beyond simply increasing speed of execution. It can lead to shorter and more readable MATLAB code. Furthermore, it expresses algorithms in terms of high-level constructs that are more appropriate for high-performance computing.
MATLAB's profiler is a useful tool when you are optimizing M-files, as it can help you decide which parts of the code to optimize. See Section 16.2 for details.
All timings in this chapter are for a 500Mhz Pentium III.
Since MATLAB is a matrix language, many of the matrix-level operations and functions are carried out internally using compiled C or assembly code and are therefore executed at near optimum efficiency. This is true of the arithmetic operators...