Speeding up multiple loops

조회 수: 2 (최근 30일)
Guan Hao
Guan Hao 2024년 7월 17일
편집: Guan Hao 2024년 7월 18일
Hi everyone,I wrote a algorithm that requires a lot of for loops.However,it's really too slow,is there any way I could speed it up?
Thanks a lot for helping me!
clear
L=0.1;
section=50;
a=L/2;
b=L/section;
v=3e8;
f1=1.2e9;
f2=4.8e9;
f3=7.5e9;
f4=10e9;
w1=(2*pi*f1);
w2=(2*pi*f2);
w3=(2*pi*f3);
w4=(2*pi*f4);
Z01=50;
Z02=75;
a0=(log(Z02/Z01))./(2.*L);
T=a;
Ub=a;
Lb=-a;
ub=a;
lb=-a;
k=5;
syms x y w
%%% Revised Algorithm
l=[0 0.01 0.03 0.05 0.07 0.1]; % Approximate sections dvided
% Approximate terms
sec=numel(l)-1;
Z1=[50 55 60 70 80 75]; % Set the impedance of the end of each sections
ac=10; % Choose the accuracy of dividing a section
for s=1:1:sec
c=(l(s+1)-l(s))/ac;
app_cos=0.5/ac*log(Z1(s+1)/Z1(s))*sin(ac/2*(w/v)*c)*cos(((ac+1)/2)*(w/v)*c)/sin(((c*w)/(2*v)));
app_sin=0.5/ac*log(Z1(s+1)/Z1(s))*sin(ac/2*(w/v)*c)*sin(((ac+1)/2)*(w/v)*c)/sin(((c*w)/(2*v)));
amp=0.75;
Ga(s)=(app_cos-(1i*amp*app_sin));
end
Ta=(1-(abs(Ga(1:1:end)).^2));
%%% Turn closed loop terms into matrix presentation
for t=1:1:sec
for r=1:1:sec
E(t,r)=Ga(t)*Ga(r);
end
end
E=triu(E,1);
for t=1:1:sec
for r=1:1:sec
D(t,r)=abs(E(r,t));
end
end
C1=sum(D);
% Construct approximate loop terms
for t=1:1:sec
D1(t)=prod(Ta(1:t))/(1+(sum(C1(1:t))));
end
D1=[1 D1(2:end)];
D1=transpose(D1)*D1;
%%% Multiply terms
tic
for s=1:1:sec % Represents the integral area of variable x
for t=1:1:sec % Represents the integral area of variable y
D=D1(s,t); % the approximate term
% Cm0
for m=1:1:k
P1=matlabFunction(((cos((m.*pi.*(y))./a)).*(cos(2.*(x-y).*w./v))).*D,"Vars",[x y w]);
P2(1,m)=integral3(P1,Lb+l(s),Lb+l(s+1),Lb+l(t),@(x)x,w1,w2);
P3(1,m)=integral3(P1,Lb+l(s),Lb+l(s+1),@(x)x,Lb+l(t+1),w1,w2);
end
P{s,t}=(P2+P3) % s section multiply t section
% Cmn
D=D1(s,t); % the approximate term
for m=1:1:k
for n=1:1:k
F=matlabFunction(((cos((m.*pi.*(y))./a)).*(cos((n.*pi.*(x))./a)).*(cos(2.*(x-y).*w./v))).*D,"Vars",[x y w]);
H1(m,n)=integral3(F,Lb+l(s),Lb+l(s+1),Lb+l(t),@(x)x,w1,w2);
H2(m,n)=integral3(F,Lb+l(s),Lb+l(s+1),@(x)x,Lb+l(t+1),w1,w2);
end
end
H{s,t}=(H1+H2); % s section multiply t section
end
end
%%% Calculate power by matrix
N=numel(P);
P1=P{1};
for m=2:1:N
P1=P1+P{m};
end
P=P1;
N=numel(H);
H1=H{1};
for m=2:1:N
H1=H1+H{m};
end
H=H1;
% am
I=inv(H);
Am=(-a0.*P)*(I)
toc

답변 (2개)

Ayush
Ayush 2024년 7월 17일
Hello @Guan Hao,
To optimise your algorithm for time and performance, you can look at some of the following approaches:
1. I can see some constants such as logZ_ratio, c_values, sin_ac2, cos_ac2 and sin_cw2v that can be precomputed or preprocessed outside of the loops to avoid redudant calculations and lead to more time taken by the algorithm.
2. For some of the nested loops to create matrices such as E and D,you could use matrix operations or vectorization approaches to make the computations more efficient. You can refer to the following documentation to know more about the uses of vectorization:
3. Lastly, to deal with multiple for loops and their performance overhead, you could leverage the "parfor" function to parallelize the outer loop if you have a multi-core system and the Parallel Computing Toolbox. You can refer to the following documentation to know more about "parfor" function:
Hope these workarounds would improve the efficiency of your algorithm and speed it up!
  댓글 수: 1
Guan Hao
Guan Hao 2024년 7월 18일
편집: Guan Hao 2024년 7월 18일
Thanks! I'll check it out.
However, I found P and H cell array take the most of the time, maybe it's integral3 taking too long.

댓글을 달려면 로그인하십시오.


Ashutosh Thakur
Ashutosh Thakur 2024년 7월 17일
Hello Guan,
You can follow different techniques to improve the performance of your code. Here are some ways:
  • Vectorization: Check if vectorization is possible in your code, as MATLAB is optimized for operations involving matrices and vectors. You can refer to the following documentation on vectorization techniques: https://www.mathworks.com/help/matlab/matlab_prog/vectorization.html.
  • Preallocation: If you are using matrices and arrays in your code, try to preallocate them instead of using dynamic resizing. Dynamic resizing is computationally expensive and can cause performance issues.
  • Use MAT files: If some operations produce constant results irrespective of the input values, you can pre-calculate these computations and use them directly in the form of MAT files to prevent re-computation.
  • MATLAB Profiler: Use the MATLAB Profiler to analyze your codebase and identify sections causing bottlenecks. This will help you focus on reducing the load in those specific areas. More information can be found here: https://www.mathworks.com/help/matlab/matlab_prog/profiling-for-improving-performance.html.
  • Parallel Computing: Utilize the Parallel Computing Toolbox to parallelize operations in your code using functions such as parfor and parfeval. You can find more information on parallel operations here: https://www.mathworks.com/help/parallel-computing/choosing-a-parallel-computing-solution.html.
Additionally, for more information on different techniques to improve performance, refer to the following documentation: https://www.mathworks.com/help/matlab/matlab_prog/techniques-for-improving-performance.html.
I hope this helps you!
  댓글 수: 1
Guan Hao
Guan Hao 2024년 7월 18일
Thank you! I'll give it a try.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by