views:

399

answers:

5

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.

+3  A: 

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

yuk
+3  A: 

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.

MatlabDoug
"Largely" is very important here :-)
AVB
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.
+2  A: 

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.

High Performance Mark
+3  A: 

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.

Loren
+2  A: 

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.

mor22
Yup. Vectorization can help performance a lot!
paradox