I have the following code in matlab and since the matrices are huge it takes more than one hour. I wanted to know where the bottle neck is and if there is any way to optimize this and make it faster.
...
for j = 1:1:2500
if (getappdata(h, 'canceling')) %Cancel waitbar
break;
end
waitbar((j * dz)/max, h, 'Calc.');
H = (stp * H')';
Intn((j)+1, :) = H .* conj(H);
xs2 = - lp(j);
xe2 = lp(j);
for m = 1:1:4097
if (x(m) < xs) && (x(m) >= start)
n(m) = 1;
elseif (x(m) > xe) && (x(m) <= ende)
n(m) = 1;
elseif (x(m) > xs2 && x(m) < xe2)
n(m) = 1;
else
n(m) = 0;
end
end
in = [(k0^2 * (n.^2 - nbar^2) - 2 * 1/stepx^2), (ran2 + 1/stepx^2 - ran2), (ran2 + 1/stepx^2 - ran2)];
P = sparse(zl, sp, in);
N = P/(2*k) + P^2/(4*k^3);
N = sparse(N);
D = eye(length(H)) + 3*P/(4*k^2) + P^2/(16*k^4);
D = sparse(D);
stp = (D + 1i*dz/2 * N)/(D - 1i*dz/2 * N);
end
where dz,xs,xe,xs2,xe2,max are Inetegers. H, x and lp are arrays of type double with 4000, 4000 and 10,000 elements. Intn is a two dimentional matrix 10,000 x 4000.
for i = 1:1:2500tofor i = 1:50, or evenfor i = 1:5. The bottleneck should be the same place regardless of the number of iterations. \$\endgroup\$