vectorised code is terribly slower
Show older comments
Why is the vectorized version of simple local maxima detection code significantly slower (~2-3 times) than its for-loop version?
%ntest data
X = rand(100000,1000);
% findig local maxima over columns of X
% for-loop version
tic;
[I,J] = size(X);
Ind = false(I,J);
for j = 1:J
Ind(:,j) = diff( sign( diff([0; X(:,j); 0]) ) ) < 0;
end
toc
% vectorized version (~3 times slower than for-loop)
tic;
Ind_ = diff(sign(diff([zeros(1,J);X;zeros(1,J)],1,1)),1,1) < 0;
toc
% result identity test
isequal(Ind,Ind_)
6 Comments
Bruno Luong
on 9 Sep 2019
I guess because
[zeros(1,J);X;zeros(1,J)]
MATLAB needs to allocate big chunk of memory (and copy segment by segment, but that happens also with for-loop).
Bruno Luong
on 9 Sep 2019
Edited: Bruno Luong
on 9 Sep 2019
Not entirely convinced. I still stick with memory related cause, because not only the verticat CAT but also DIFF, SIGN, DIFF create 3 big temporary arrays (hidden).
If you add 1,1 parameter in for-loop
tic;
[I,J] = size(X);
Ind = false(I,J);
for j = 1:J
Ind(:,j) = diff( sign( diff(array(:,j),1,1) ),1,1) < 0;
end
toc
it's still fast. How do you explain that?
You note also that the reative difference of CPU times is less if you reduce the first dimension of X.
Bruno Luong
on 9 Sep 2019
It is possibly that the DIFF implementation on array does not access sequently memory in case of 2D array data, but row-by-row of the array, that might slow down.
I don't think the multi-threading is wrongly implemented.
Michal
on 9 Sep 2019
Answers (0)
Categories
Find more on Execution Speed in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!