4

I have been working lately on a number of iterative algorithms in MATLAB, and been getting hit hard by MATLAB's performance (or lack thereof) when it comes to loops. I'm aware of the benefit of vectorizing code when possible, but are there any tools for optimization when you need the loop for your algorithm?

I am aware of the MEX-file option to write small subroutines in C/C++, although given my algorithms, this can be a very painful option given the data structures required. I mainly use MATLAB for the simplicity and speed of prototyping, so a syntactically complex, statically typed language is not ideal for my situation.

Are there any other suggestions? Even other languages (python?) which have relatively painless matrix tools are an option.

1
  • Showing the code and highlighting the slow bits would really help. There are many techniques available that might not be spoken of in the general advice. Ideally, we could just copy and paste your code to see it work without need for data files and such.
    – MatlabDoug
    Mar 2, 2010 at 15:04

5 Answers 5

4

It was once true that vectorization would improve the speed of your MATLAB code. However, that is largely no longer true with the JIT-accelerator

This video demonstrating the MATLAB profiler might help.

3
  • 1
    "Largely" is very important here :-)
    – AVB
    Mar 2, 2010 at 2:19
  • I am aware of using the profiler...that would have been a helpful addition to my original question :) That said, the particular behavior of the JIT accelerator is new to me. Seems a shame that Mathworks has hidden its behavior from users. Thanks for the link anyway, please let me know if you have any other suggestions.
    – user262063
    Mar 2, 2010 at 7:55
  • "JIT-accelerator" link is dead, is there any other link to this article? Mar 15, 2013 at 13:29
4

PROFILER is very useful tool to find bottlenecks in Matlab code. it does not change your code of course, but helps to find which functions/lines to optimize with vectorization or mex.

http://www.mathworks.com/access/helpdesk/help/techdoc/ref/profile.html

http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_env/f9-17018.html

3

If you have a choice, be sure to set up your loops so you scan the data column-wise which is how the data in MATLAB are arranged. In addition, be sure to preallocate any output arrays before the loop and index into them instead of growing the array inside the for-loop.

3

If you can cast your code so your operations are called on the whole matrix then you will see great improvement in the speed of your code. Many functions are much quicker when operating on the whole matrix rather than in an element-wise fashion with loops.

0
3

You might want to investigate MATLAB's Parallel Computing Toolbox which can make a big difference if you have the right hardware. I re-wrote about 12 lines of code and got 4 - 6 times speedup for one of our loop-intensive programs on and eight core PC.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.