Speed optimisation in matlab with functions and large arrays

There are some speed optimisations which are obvious in matlab:

  • always pre-allocate your array
  • use implicit vector notation if possible
  • don’t set loop indexes or tests which require the creation of large arrays every loop iteration

But one of the ones that was less obvious, at least to me, was the concept in Matlab of passing variables to functions as references OR as values.

I came from the FORTRAN and C world, and in my mind you always passed pointers or references to a function, so it didn’t matter if you were passing a large vector to a function, editing it inside the function, and then taking it back out again, as all you would do is edit the value pointed at by the pointer.

In Matlab things are different. Variables are only passed as references (which is what you want) IF the variable is NOT changed in the function. For the sake of argument, say that you have a long vector called VEC in your main script, that you pass to a function. That function must calculate one new scalar which will be added to your vector : VEC(i)=Y. There are two ways to do this:

  • You change VEC(i) in the function : VEC(i) = Y. In this case the whole vector VEC needs to be copied into a new variable, just to change one cell. This means more computation time. You do not want that, even though it might improve legibility of your code.
  • You compute the new scalar Y inside the function, and then output that back to your main script (you do NOT edit VEC(i) in the function). In your main script you run VEC(i) = Y. In this case your VEC is never copied, and your code is optimized (albeit perhaps less legible).

See this very good write up for more details http://gribblelab.org/scicomp/10_Speeding_up_your_code.html