|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?
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 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.
|Note If you have a loop in your code that should accelerate but does not show much improvement, check to see that you are using preallocation rather than growing your arrays on each iteration of the loop.|
|What MATLAB Does Not Accelerate||What to Avoid When Running MATLAB|