Programming and Data Types    

How Vectorizing and Preallocation Fit In

With many programs being automatically accelerated by MATLAB, how important is it to use other optimizing techniques discussed in this chapter, such as vectorizing and preallocation?

Vectorizing Loops

When processing code that contains loops, the MATLAB performance accelerator does the equivalent of implementing the loops with vectorized code. If you have already vectorized your programs, you will not see a significant performance improvement in these programs when run with the accelerator.

However, greater speed in executing loops means that you now have a choice of whether to vectorize your code or not. Either way, you will get about the same performance from MATLAB. Choose the form that is the most understandable or that best fits the application.

Preallocating Arrays

Preallocating arrays keeps MATLAB from having to repeatedly find and allocate contiguous storage as arrays expand in size during program execution. Therefore, it continues to be just as important with performance acceleration as in earlier versions without this feature.


  What MATLAB Does Not Accelerate What to Avoid When Running MATLAB