I had to write some MATLAB code, and at some point I had to apply a function I wrote elementwise to a vector. I considered two different way to do that:
- Loop over the vector
- Use MATLAB elementwise operations
In my case, I have a function group
defined like:
function [g] = group(n, elm_per_group, n_groups)
g = mod(n, n_groups);
g(g > elm_per_group) = -1;
end
So that case #1 is like:
N = 1e4;
v = (1:N)';
w = zeros(1, N);
for i = 1 : N
w(i) = group(v(i), elm_per_group, n_groups);
end
And case #2:
N = 1e4;
v = (1:N)';
w = group(v(:), elm_per_group, n_groups);
Of course it is absolutely not optimized to do that, except for the sake of the example.
I found very interesting things here and here, but I still can't understand what mechanisms MATLAB uses to speed up the computation, given an arbitrary function, which could even include randomness or have some kind of characteristic that will require specific calculation for each element of the vector.
I already have two ideas that explain, to me, part of the difference:
- As MATLAB indexing is quite slow, so that looping is not a efficient way to reach every elements in the vector
- The profiler tells that when looping over the vector, the
group
function has been calledN
times, whereas it is called only once when using MATLAB built-in. I expect that at least the time to manage the stack and the function call is shortened.
I'm not conviced that these two arguments are enough to explain the gap between the two approaches. To give some numbers, case #1 takes 7.2 ms and case #2 0.84 ms.
So, what are the additionnal mechanisms used by MATLAB to speed up the computation of N
times the same function on the elements of a vector ?
(Note: please report spelling mistakes so that I can correct them.)
EDIT 1: changed name group
to g
to avoid name clash.
EDIT 2: modified the group
function so that it is less bad and actually works.