Say I want to multiply each element of a cell array A
with a coefficent k
. I can do that by:
<
Not exactly an answer, but here is a way to see the affect of JIT compiler and accelerator in both approaches (cellfun vs. for-loop):
feature('jit', 'off'); feature('accel', 'off');
tic, A = cellfun(@(x) k*x, A, 'UniformOutput', false); toc
tic, for i=1:numel(A), A{i} = A{i}*k; end, toc
feature('jit', 'on'); feature('accel', 'on');
tic, A = cellfun(@(x) k*x, A, 'UniformOutput', false); toc
tic, for i=1:numel(A), A{i} = A{i}*k; end, toc
I get the following
Elapsed time is 25.913995 seconds.
Elapsed time is 13.050288 seconds.
vs.
Elapsed time is 10.053347 seconds.
Elapsed time is 1.978974 seconds.
with optimization turned on in the second.
By the way, parallel parfor
performed much worse (at least on my local test machine with a pool size of 2 processes).
Seeing the results you posted, MEX-function is the way to go :)