When writing both in Octave and in MATLAB, arrayfun is encouraged, in order to not create brevity in the code, but also speed. This is unlike what is in the discussion of the following node, which only talks about styling, not computation performance.
See Octave code below:
function ret = vect_vs_array_fun(n)
a=1:n;
tic;
for i=1:n
a(i)=sin(i)/7;
endfor
toc;
a=1:n;
tic;
a=arrayfun( @sin, 1:n ) / 7;
toc;
a=1:n;
tic;
for i=1:n
a(i)=sin(i)/7;
endfor
toc;
endfunction
When running the function for a large enough value, one can see the runtime difference:
vect_vs_array_fun(100000)
Elapsed time is 0.627594 seconds.
Elapsed time is 0.122411 seconds.
Elapsed time is 0.623537 seconds.
The more nested for loops are replaced by arrayfun
, the relative faster it will get.
The question is: is there something equivalent, performance-wise for Python/NumPy, and in particular, its array?