Matlab: vectorization and profiler

Matlab sometimes can be a disaster in terms of speed. Especially when I don’t account for its matrix-vector inner nature from the beginning and do for loops instead of matrix operations.

I had a small matlab script that often calls very simple computation of the exponential function. In order to check whether it was a kind of bottle-neck I gave a try to Matlab Profiler. I followed the lines of

In my case I have realized that too much time is spent in for loops (indeed). Substituting them (whenever possible) with vector operations already gave 5 times faster performance. Another issue raised by profiler was that the internal function polyval was somehow slow in my case. Going into the code of that and creating my own version without useless checks (since in my case I am sure about which kind of data is sent to the function) made another 60-80% of the acceleration.

Two important things for me out of that story: 1) use vectorization in matlab 2) use profiler to see the real picture of the performance

Couple of useful resources on vectorization and optimization:

Leave a Reply