Basic Matlab Code Optimization

May 17, 2011

As the saying goes, “In a day of work, the best programmers delete more lines of code than they write.” Condensing code usually decreases the run time of a program. Generally, the most time can be saved in the lines of code which are called most often. These slow lines are called bottlenecks. For a beginner it is best to determine the bottlenecks of the software by using the profile tool in Matlab. The profile results help you focus on the most time consuming lines of code. Once you gain more experience you can profile less, but I still find it very addicting to measure the success of each major optimization.

Before you optimize complete the development of the code. Optimization can obfuscate code, so I recommend that a program is designed for readability until it is stable and complete. This will make debugging and changing the solution much easier during development.

Variable Declarations/Sizing

Matlab does not require the declaration of variables. However, Matlab’s variables are stored as arrays and each time the dimensions of the array change Matlab has to reallocate memory for the variable. Matrix size often changes in for-loops. To prevent this you can define the variable’s size initially and save some time. You can use the zeros or ones function to fill a variable.

Defines x as a square 3 x 3 matrix filled with zeros:

x = zeros(3);

0 0 0
0 0 0
0 0 0

Defines y as a 1 x 3 matrix filled with ones:

y = ones(1,3);

1 1 1

Eliminate For-Loops

For-loops can consume A LOT of time. Instead of for loops use matrix operators for addition, subtraction, multiplication and division. Recall from your math classes that matrix multiplication and division will not apply an element-wise multiplication of two matrices. However Matlab provides a method to perform multiplication between the corresponding cells of two matrices. To do element-element operations, use the element-wise Matlab operators. See an example here.

Element-wise operators:

.* - Multiplication
./ - Division
.^ - Power

Example of removing a for-loop:

Where a, b, t & p are arrays:

For-Loop code:

b = zeros(1000000,1);
for i=1:1000000
b(i) = a(i)*t(i)/p(i);
end

Optimized Code:

b = a.*t./p;

The reduction in speed is impressive. I ran each calculation with profiler. Lines 7-10 are pre-optimized code and line 5 is the optimized code. Each give the same answer. These are the results: (time(sec) is in red).

profile of for-loop optimization

Functions

Functions are nice for organizing code and enhancing readability, but they come with costly overhead. The overhead comes from passing variables into a function and reassigning these values to the child function’s local variables. Placing the function’s code inline with the parent function’s code will save a significant amount of time. Built in Matlab functions can also be placed inline with your code. One large .m file will be much quicker than a number of smaller .m files.

Structured Arrays

I have come to enjoy the organization structured arrays offer. Unfortunately their high level of organization require additional referencing time for Matlab. Replace a structured array with a number of single arrays to cut down on this referencing time.

Unused Code

It may be surprising, but some lines of code may never be utilized in your program. Use the profile tool to determine if you have any code that is not run. Ask yourself if these lines of code exist to make you feel better, or if they actually serve a purpose and will be utilized in the future.

Tags: , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*