I have multiple for loops and the code is slow. How can I speed it up?
30 ビュー (過去 30 日間)
古いコメントを表示
Here is a snippet of the code below. The total code takes around 160s to run and I know For loop are not optimal for Matlab but I am not sure what the alternative is. The purpose of the code is to determine the power necessary to meet the If condition in the first loop and then go to the second loop and determine the system capacity based on the power remaining.
If anyone can help on tips to speed it up that would be great.
for FR = FR
PD1 = 0; PD_Ideal_evapTD = 0;
Td = Tmin;
Qevap = ((FR/3600)*(Hvap)*MC)+(MC*FR*heat/3600);
for j = 0:dx:SA %x = [L/4 L/4 L/4 L/4]
Td = Td + dTdA*dx;
R1 = RT*j/SA;
D_evap = Depthevap(R1,FR,MC);
lambda_peak = 2.8977719./(10.*(Td));
lmin = 0.5*lambda_peak*1e-2; lmax = 2*lambda_peak*1e-2;
PD1 = PD1 + integral(@(l)plank_evap(l,Td,D_evap,T),lmin,lmax)*dx*top_deck*1;
PD_Ideal_evapTD = PD_Ideal_evapTD + integral(@(l)plank_ideal(l,Td,D,T),lmin,lmax)*dx*top_deck;
delta_x = j;
if (PD1/1000) >= Qevap/2
break
end
end
i = i+1;
dx2 = [dx2 ceil(N*delta_x)/N];
dxr = ceil(N*delta_x)/N;
PD2 = 0;PD_Ideal_TD = 0; PD_Ideal2 = 0;
k = 1;
R2 = (1-dxr/SA)*RT;
D = Depth(R2,Tp(k),Y2,FR,MC);
for x = dxr:dx:SA
Td = Td + dTdA*dx;
lambda_peak = 2.8977719./(10.*(Td));
lmin = 0.5*lambda_peak*1e-2; lmax = 2*lambda_peak*1e-2; %m
PD2 = PD2 + integral(@(l)plank(l,Td,D,T),lmin,lmax)*dx*top_deck*1; % W
PD_Ideal_TD = PD_Ideal_TD + integral(@(l)plank_ideal(l,Td,D,T),lmin,lmax)*dx*td; % W
PD_Ideal2 = PD_Ideal2 + integral(@(l)plank_ideal(l,Td,D,T),0.01*lmin,100*lmax)*dx*td; % W
end
k = k+1;
i = i+1;
P_evap_TD = [P_evap_TD PD1/1000];
P_Ideal_evapTD = [P_Ideal_evapTD PD_Ideal_evapTD/1000];
P_TD = [P_TD PD2/1000];%kW
P_Ideal_TD = [P_Ideal_TD PD_Ideal_TD/1000]; %kW
P_Ideal2 = [P_Ideal2 PD_Ideal2/1000];
TransferPower_TD = (P_TD + P_evap_TD)/(P_Ideal_evapTD + P_Ideal_TD);
Factor1 = PD_Ideal_TD/PD_Ideal2;
end
Pavail_TD = P_TD + P_evap_TD;
6 件のコメント
Cris LaPierre
2023 年 8 月 3 日
In order to vectorize a for loop, all operations inside the loop must be able to be done as matrix or vector operations. If not, then you may have to use a for loop.
回答 (1 件)
Sai Teja G
2023 年 8 月 23 日
Hi Matt,
I understand that you want to speed up your execution time of your code.
There are a couple of workarounds to reduce the execution time:
- One approach is to vectorize your for loops, which can speed up the execution. You can find more information on how to vectorize loops in this link: Vectorization of loops.
- Instead of using regular for loops, you can utilize parallel for-loops. This allows the loops to run in parallel, leading to a decrease in execution time. For more details on parallel for-loops, you can refer to this link: Parallel for-loop.
These techniques can help optimize the execution time of your code and improve its performance.
Hope it helps!
0 件のコメント
参考
カテゴリ
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!