Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Methods to Improve the Calculation Speed of MATLAB Programs - Part 2

Improving Performance of the Code

MATLAB programs are interpreted. Hence large Matlab programs will take a long time to execute. The power of MATLAB is realized with its extensive set of libraries which are compiled or are carefully coded in MATLAB to utilize 'vectorization'.
The concept of vectorization will help to write efficient MATLAB code. The speed of a numerical algorithm in MATLAB is very sensitive to whether or not vectorized operations are used.

This section presents some basic considerations to writing efficient MATLAB routines. It is possible, of course, to become obsessed with performance and waste man-hours of programming time in order to save a few seconds of execution time. The ideas presented below require little extra effort, yet can yield significant speed improvements. Once you understand how to use these procedures they will become natural parts of your MATLAB programming style.


Using vector operations instead of loops


MATLAB is a matrix language, which means it is designed for vector and
matrix operations. You can often speed up your M-file code by using vectorizing
algorithms that take advantage of this design. Vectorization means converting
for and while loops to equivalent vector or matrix operations.
Simple Example of Vectorizing
Here is one way to compute the sine of 1001 values ranging from 0 to 10:
i = 0;
for t = 0:.01:10
i = i + 1;
y(i) = sin(t);

end
A vectorized version of the same code is
t = 0:.01:10;
y = sin(t);
The second example executes much faster than the first and is the way
MATLAB is meant to be used. Test this on your system by creating M-file
scripts that contain the code shown, and then using the tic and toc functions

to time the M-files.




Consider the following loop, translated directly from Fortran or C
         dx = pi/30;
         nx = 1 + 2*pi/dx;
         for i = 1:nx
            x(i) = (i-1)*dx;
            y(i) = sin(3*x(i));
         end
The preceding statements are perfectly legal MATLAB statements, but they are an inefficient way to create the x and y vectors. Recall that MATLAB allocates Memory for variables on the fly. On the first time through the loop (i=1), MATLAB creates two row vectors x and y, each of length one. On each subsequent pass through the loop MATLAB appends new elements to x and y. Not only does this incur extra overhead in the memory allocation calls, the elements of x and y will not be stored in contiguous locations in RAM. Thus, any subsequent operations with x and y, even though these operations may be vectorized, will take a performance hit because of memory access overhead.
The preferred way to create the same two x and y vectors is with the following statements.
         x = 0:pi/30:2*pi
         y = sin(3*x);
The first statement creates the row vector, x, with 1 + pi/15 elements stored in contiguous locations in RAM. The second statement creates a new (matrix) variable, y, with the same number of rows and columns as x. Since x is a row vector, as determined by the preceding step, y is also a row vector. If x were, for example, a 5 by 3 matrix, then y = sin(3*x) would create a 5 by 3 matrix, y.
MATLAB is designed to perform vector and matrix operations efficiently. To take maximum advantage of the computer hardware at your disposal, therefore, you should use vectorized operations as much as possible.

Pre-allocating memory for vectors and matrices

for and while loops that incrementally increase, or grow, the size of a data
structure each time through the loop can adversely affect performance and
memory use. Repeatedly resizing arrays often requires that MATLAB spend
extra time looking for larger contiguous blocks of memory and then moving the
array into those blocks. You can often improve on code execution time by
preallocating the maximum amount of space that would be required for the

array ahead of time.
The following code creates a scalar variable x, and then gradually increases the
size of x in a for loop instead of preallocating the required amount of memory
at the start:
x = 0;
for k = 2:1000
x(k) = x(k-1) + 5;
end
Change the first line to preallocate a 1-by-1000 block of memory for x initialized
to zero. This time there is no need to repepatedly reallocate memory and move
data as more values are assigned to x in the loop:
x = zeros(1, 1000);
for k = 2:1000
x(k) = x(k-1) + 5;

end




Though MATLAB will automatically adjust the size of a matrix (or vector) it is usually a good idea to pre-allocate the matrix. Pre-allocation incurs the cost of memory allocation just once, and it guarantees that matrix elements will be stored in contiguous locations in RAM (by columns).
Consider the following (admitedly artificial) sequence of statements.
         dx = pi/30;
         nx = 1 + 2*pi/dx;
         nx2 = nx/2;
         for i = 1:nx2
            x(i) = (i-1)*dx;
            y(i) = sin(3*x(i));
         end
         for i = nx2+1:nx
            x(i) = (i-1)*dx;
            y(i) = sin(5*x(i));
         end
Since we know the size of x and y, a priori, we can pre-allocate memory for these vectors. Pre-allocation involves creating a matrix (or vector) with one vectorized statement before any of the matrix elements are referenced individually. The ones and zeros functions are typically used to pre-allocate memory.
Here is an improvement of the preceding statements with pre-allocation of memory for x and y.
         dx = pi/30;
         nx = 1 + 2*pi/dx;
         nx2 = nx/2;
         x = zeros(1,nx);      % pre-allocate row-vectors, x
         y = zeros(1,nx);      % and y
         for i = 1:nx2
            x(i) = (i-1)*dx;
            y(i) = sin(3*x(i));
         end
         for i = nx2+1:nx
            x(i) = (i-1)*dx;
            y(i) = sin(5*x(i));
         end
The statements x(i) = ..., and y(i) = ... still do not take advantage of possibilities for vectorization, but at least the elements of x and y are stored contiguously in RAM. We will improve the efficiency of the loops shortly. First, however, note that we could have written the pre-allocation statements as
         x = zeros(1,nx);      % pre-allocate row-vectors, x
         y = x;                % and y
The statement y = x does not mean that y will stay equal to x. It simply creates another matrix y with the same ``shape'' as x. Understanding that pre-allocation is important for efficiency will help you understand these apparently confusing twists of MATLAB programming logic.
We can further improve our loops by pulling the assignment of x out of the loops.
         x = 0:pi/30:2*pi;     % vectorized calculation of x
         nx = length(x);
         nx2 = nx/2;
         y = x;                % pre-allocate memory for y
         for i = 1:nx2
            y(i) = sin(3*x(i));
         end
         for i = nx2+1:nx
            y(i) = sin(5*x(i));
         end
Finally, if we're obsessed with performance, we observe that the calculation of y can also be vectorized. To do this we use the colon notation to refer to segments of the x and y vectors.
         x = 0:pi/30:2*pi;     % vectorized calculation of x
         nx = length(x);
         nx2 = nx/2;
         y = x;                             % pre-allocate memory for y
         y(1:nx2) = sin(3*x(1:nx2));        % compute first part of y
         y(nx2+1:nx) = sin(5*x(nx2+1:nx));  % and the second part
To those new to MATLAB programming, the preceding statements may appear unecessarily obfuscated. The comment statements, of course, help, but the logic behind the logic comes from a true understanding of vectorization. Once you get the hang of MATLAB's colon notation you too will come to write code like this. Whenever the speed of MATLAB code is important, there is no substitute for vectorization.


Coding Loops in a MEX-File
If there are instances where you cannot vectorize and must use a for or while
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.

Functions Are Generally Faster Than Scripts
Your code executes more quickly if it is implemented in a function rather than
a script.
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 low-level
MATLAB file I/O routines such as fread and fwrite, choose the former. load
and save have been optimized to run faster and reduce memory fragmentation.
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.

Clearing Unused Variables from Memory
If you use pack and there is still not enough free memory to proceed, you
probably need to remove some of the variables you are no longer using from

memory. Use clear to do this.


Correct code is always more important than speed

Vectorization sometimes makes MATLAB code hard to read. If you find yourself puzzling over the code, or more importantly if you find yourself wondering if the code performs the correct calculation, then stop trying to optimize performance.
Always remember


Code that gives incorrect, or inaccurate results is useless, no matter how fast it executes.


This post first appeared on Electrical Engineering Tutorial, please read the originial post: here

Share the post

Methods to Improve the Calculation Speed of MATLAB Programs - Part 2

×

Subscribe to Electrical Engineering Tutorial

Get updates delivered right to your inbox!

Thank you for your subscription

×