For loop and computational time

1 Ansicht (letzte 30 Tage)
muhammad imran
muhammad imran am 22 Okt. 2020
Bearbeitet: muhammad imran am 22 Okt. 2020
My code is taking much longer to run. Is there any way I can make it faster.
Here is my code.
clear td to w d m phi wp dk Ham Hu Idd Gh huu e2 x t T H
clear p q h qmax alpha A1 q1 Q aalpha g hd Hf L aaa k1 k2 k td tdd j ja i j1 a H T v all_v e e1
tic
td = 1;
phi = (2*pi)/3;
w = 110;
qmax = 120;
for i = 1:1:qmax
if i>2
q(i)=i;
for j = 1:1:i
p(j) = j-1;
A1(i,j)=sym(p(j)/q(i));
end
end
end
alpha = unique(reshape(A1',1,[]));
[p,q1] = numden(sym(alpha));
for i = 1:1:length(alpha)
if double(q1(i))>2
Q(i) = double(q1(i));
aalpha(i) = double(alpha(i));
else Q(i) = 3;
aalpha(i)= 1/3;
end
end
figure1 = figure;
axes1 = axes('Parent',figure1)
hold(axes1,'all');
for i= 1:1:length(aalpha)
H = zeros(2*Q(i),2*Q(i));
L1 = 4*pi*aalpha(i);
L2 = 2*pi/(double(Q(i)));
k1 = 0:L1/10:L1;
k2 = 0:L2/10:L2;
for l1 = 1:1:length(k1)
for h2 = 1:1:length(k2)
for j = 1:2:2*Q(i)-1
H(j,j+1)=w*td*exp(-2*pi*(aalpha(i)/sqrt(3))-1i*(1*k1(l1)+4*pi*aalpha(i)*(j-1)/2));
H(j+1,j)=conj(w*td*exp(-2*pi*(aalpha(i)/sqrt(3))-1i*(1*k1(l1)+4*pi*aalpha(i)*(j-1)/2)));
end
for j = 2:2:2*Q(i)-2
H(j+1,j)=conj(w*td*exp(1i*phi)*exp(-2*pi*(aalpha(i)/sqrt(3))+1i*(1*k1(l1)/2+1*pi*aalpha(i)*((2*(j-2)/2)+1))-1i*1*k2(h2)));
H(j,j+1)=w*td*exp(1i*phi)*exp(-2*pi*(aalpha(i)/sqrt(3))+1i*(1*k1(l1)/2+1*pi*aalpha(i)*((2*(j-2)/2)+1))-1i*1*k2(h2));
end
for j = 1:2:2*Q(i)-3
H(j,j+3)=w*td*exp(-1i*phi)*exp(-2*pi*(aalpha(i)/sqrt(3))+1i*(1*k1(l1)/2+1*pi*aalpha(i)*(2*((j-1)/2)-1))+1i*1*k2(h2));
H(j+3,j)=conj(w*td*exp(-1i*phi)*exp(-2*pi*(aalpha(i)/sqrt(3))+1i*(1*k1(l1)/2+1*pi*aalpha(i)*(2*((j-1)/2)-1))+1i*1*k2(h2)));
end
H(1,2*double(Q(i))) =1* w*td*exp(-1i*phi)*exp(-2*pi*(aalpha(i)/sqrt(3))+1i*(1*(k1(l1)/2)+1*pi*aalpha(i)*(2*double(Q(i))-1))+1i*1*k2(h2));
H(2*double(Q(i)),1) = conj(1* w*td*exp(-1i*phi)*exp(-2*pi*(aalpha(i)/sqrt(3))+1i*(1*(k1(l1)/2)+1*pi*aalpha(i)*(2*double(Q(i))-1))+1i*1*k2(h2)));
H(2*double(Q(i))-1,2) =1*conj(w*td*exp(1i*phi)*exp(-2*pi*(aalpha(i)/sqrt(3))+1i*(1*(k1(l1)/2)+1*pi*aalpha(i)*(2*(double(Q(i)))+1))-1i*1*k2(h2)));
H(2,2*double(Q(i))-1) = 1*w*td*exp(1i*phi)*exp(-2*pi*(aalpha(i)/sqrt(3))+1i*(1*(k1(l1)/2)+1*pi*aalpha(i)*(2*(double(Q(i)))+1))-1i*1*k2(h2));
e2= eig(H);
hold on
plot(aalpha(i),e2,'.','MarkerSize',1);
end
end
end
hold off
%saveas(figure1,'Perfect.pdf')
timeelapsed = toc
  2 Kommentare
Rik
Rik am 22 Okt. 2020
The first step would probably be to use the profiler to determine the bottlenecks. Did you do that?
per isakson
per isakson am 22 Okt. 2020
Bearbeitet: per isakson am 22 Okt. 2020
Replacing parfor by for seems to save a lot of time ( on my system ).

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Raymond Norris
Raymond Norris am 22 Okt. 2020
Hi Muhammad,
I would suggest provide a bit more information when it comes to performance questions
  • Which version of MATLAB?
  • How many cores?
  • How much memory?
  • How long does it take to run your code?
  • How long would you like it to take?
For me, your code appears to take less then 2s to run on 4 cores. Might be tough to eack out a lot more. A couple of suggestions
  • The MATLAB Code Analyzer highlights a few suggestions (e.g. preallocation). I would suggest going through each of the recommendations.
  • You might see slightly better performance writing this as a function (even without input/outputs) then a script
  • As it's written, you can't plot figures within a parfor -- keep in mind, the body of the code is being executed on a separate worker(s) that are not sharing the MATLAB clients figure window. If you want to plot with a parfor, look at setting up a parallel.pool.DataQueue. In fact, one of the examples in the doc for it shows updating a waitbar.
Raymond
  1 Kommentar
muhammad imran
muhammad imran am 22 Okt. 2020
Bearbeitet: muhammad imran am 22 Okt. 2020
@Raymond: This is because by mistake I have put qmax equal to 3. But for real run it will be qmax = 120. This means the dimension of the matrix go upto 120 times 120. I have I-7 processor with 6 cores, and 16 GB RAM.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by