For loop and computational time
조회 수: 1 (최근 30일)
이전 댓글 표시
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
Rik
2020년 10월 22일
The first step would probably be to use the profiler to determine the bottlenecks. Did you do that?
per isakson
2020년 10월 22일
편집: per isakson
2020년 10월 22일
See profile, Profile execution time for functions
Replacing parfor by for seems to save a lot of time ( on my system ).
답변 (1개)
Raymond Norris
2020년 10월 22일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!