vectorised code is terribly slower
显示 更早的评论
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 个评论
Bruno Luong
2019-9-9
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
2019-9-9
编辑:Bruno Luong
2019-9-9
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
2019-9-9
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
2019-9-9
回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Execution Speed 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!