I was thinking that for loops were usually slower than vector or matrix operations, but a simple example showed me the opposite.
Here is a comparison of different implementations of a counter. The results for five runs of the code below are the following:
I am surprised to see that the function equation1 (multiplication of a vector by a scalar) is slower than counter (for loop).
I am also surprised to see that the function equation1 is slower than equation2, just because of the vector is stored in a variable.
Does anyone have an explanation?
f1 = @() equation1(step, N);
f2 = @() equation2(step, N);
f3 = @() counter(step, N);
function out = equation1(step, N)
function out = equation2(step, N)
function out = counter(step, N)
out(n) = out(n-1) + step;