Friday, March 30, 2012

A tip to speed up your MATALB script

A few days ago, my friend sent me a MATLAB script asking for help.
The script had a bug, but he said if the bug is fixed it would take a few hours to run.
I fixed the bug and made some changes, then it took less than 20 seconds to run.
How could this be?

Here's a small tip that can make a huge change in calculation time.

Let's say we are calculating something and updating an array. For example, you start with an array with size (10,1). During the for-loop or while-loop you're adding one more value to it.
So the array size becomes (11,1). This is what happens behind the scene.

Click on the picture to enlarge.

When you ask for one more space that's not ready to use yet, MATLAB has to get that extra space from Windows. (follow the orange arrows). Windows wants to keep elements of an array together, physically. So it goes to the memory to see if there's a room or one more element.

If there is, it gives ok sign to MATLAB (still following the orange arrows), and MATLAB can use it.
If there isn't, Windows has to find a space somewhere else on the memory (not we're following the red arrows). Once that relocation is done, it gives ok sign to MATLAB.

This can be avoided by declaring an empty array before you use it. Like, A=zeros(11,1). Now the space is available and initialized with zeros. When your MATLAB script wants to use that 11th space, it can just go there and use it (blue arrows).

The yellow and red arrows shown above takes much longer time compared to the blue route, and becomes really noticeable when you deal with large array size.

If your array is expanding from (10,1) to (11,1), you won't notice a difference, but if you're expanding from (100000,1) to (100001,1) then (100002,1) and so on, you may end up spending hours instead of minutes.

So guesstimate the array size you'll need and initialize it.
MATLAB is very forgiving in many aspects, but that can slow down your code.




No comments:

Post a Comment