vectorised code is terribly slower

2 ビュー (過去 30 日間)
Michal
Michal 2019 年 9 月 9 日
コメント済み: Michal 2019 年 9 月 9 日
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
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
Michal 2019 年 9 月 9 日
The main problem is, that during continuous development of JIT engine are alwyas changing MATLAB performance characteristics for vectorized codes. In general, the standard for-loop codes becomes faster and faster.
I have plenty of highly vectorized MATLAB codes created during last 10 years, which are during last few years becomes slower than theirs for-loop counter parts. So, there is no code performance stability.

サインインしてコメントする。

回答 (0 件)

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by