This question is a bit an offspring from an other one, but I have the following two codes:
maxN = 100;levels = maxN+1;xElements = 101;umn = complex(zeros(levels, levels)); % cleaning
bessels = ones(1201, 1201, 101); % 1.09 GB
negMcontainer = ones(1201, 1201, 100);posMcontainer = negMcontainer;ticfor j = 1 : xElements for i = 1 : xElements for n = 1 : 2 : maxN nn = n + 1; mm = 1; m = 1:2:n; numOfEl = ceil(n/2); umn(nn, mm:mm+numOfEl-1) = bessels(i, j, nn) * posMcontainer(i, j, m); end endendtocticfor j = 1 : xElements for i = 1 : xElements for n = 1 : 2 : maxN nn = n + 1; mm = 1; for m = 1 : 2 : n umn(nn, mm) = bessels(i, j, nn) * posMcontainer(i, j, m); mm = mm + 1; end end endendtoc
And it tourns out, that loops version is faste >2x. Why is that so? I know that i happens if vectorization requiers large temporary variables, but (it seems) it is not true here.
And generally, what (other than parfor) can I do to speed up this code?
Best regards, Alex
Best Answer