Clear Filters
Clear Filters

I have multiple for loops and the code is slow. How can I speed it up?

17 views (last 30 days)
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 Comments
Matt R
Matt R on 3 Aug 2023
@Dyuman Joshi Even when I pre-allocate accoridng to the link, the result is the same and the code takes too long. Perhaps I am too impatient but the timer is around 4-5mins.
It's not an error - it's a suggestion from the red line under the variable. "Variable appears to change size on every loop iteration. Consider preallocating for speed."
@Cris LaPierre I have tried vectorization for the outer For loop in the code above (starting "For FR =FR...") and I get the following error:
Error using ()
Query coordinates input arrays must have the same size.
Error in interp2 (line 156)
Vq = F(Xq,Yq);
Error in transmittance_extrapolater_vap (line 6)
p = interp2(D1,v1,T,D_evap,v);
Error in PlanckslawTorrcoal>plank_evap (line 262)
Tw1 = transmittance_extrapolater_vap(T,D_evap,v/100)';
Error in PlanckslawTorrcoal>@(l)plank_evap(l,Td,D_evap,T) (line 92)
PD1 = PD1 + integral(@(l)plank_evap(l,Td,D_evap,T),lmin,lmax).*dx.*top_deck.*1; % W
Error in integralCalc/iterateScalarValued (line 314)
fx = FUN(t);
Error in integralCalc/vadapt (line 132)
[q,errbnd] = iterateScalarValued(u,tinterval,pathlen);
Error in integralCalc (line 75)
[q,errbnd] = vadapt(@AtoBInvTransform,interval);
Error in integral (line 87)
Q = integralCalc(fun,a,b,opstruct);
Error in PlanckslawTorrcoal (line 92)
PD1 = PD1 + integral(@(l)plank_evap(l,Td,D_evap,T),lmin,lmax).*dx.*top_deck.*1; % W
Cris LaPierre
Cris LaPierre on 3 Aug 2023
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.

Sign in to comment.

Answers (1)

Sai Teja G
Sai Teja G on 23 Aug 2023
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:
  1. 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.
  2. 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!

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!