MATLAB cellfun vectorization slow when using function handle

Viewed 60

I encountered a weird bug in cell vectorization (MATLAB version R2019B).

Please consider the following minimal example, say we generate a cell array with variable length vector in each cell:

N = 10000;
rng(1);
result = cell(N,1);
numConnect = randi(10, [N,1]); % randomly generated number of connected nodes
for i = 1:N
  result{i} = randi(N, [1, numConnect(i)]);
end

Now we want to retrospectively retrieve numConnect, i.e., the length of each cell, we can use cellfun. According to this documentation, in Backward Compatibility mode, you can use string as func variable instead of function handle. However, there is a drastic difference in performance locally.

tic;
nC1 = cellfun('length', result);
toc;

This one usually produces something like

Elapsed time is 0.038531 seconds.

If I changed to @ function handle:

tic;
nC2 = cellfun(@length, result);
toc;

Then

Elapsed time is 1.041925 seconds.

is normal. There is a 30x difference!

I wonder is this performance difference a bug on my local machine, or a "feature" of MATLAB cellfun?

0 Answers
Related