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!