I have multiple for loops and the code is slow. How can I speed it up?
Show older comments
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
Dyuman Joshi
on 2 Aug 2023
"I know For loop are not optimal for Matlab"
That is incorrect. If used properly, for loops are quite efficient.
It seems you are computing the same integral twice in both the for loops, which could be taxing on the speed of the code. I suggest you compute it once and store it in a variable and use the variable wherever required.
It's difficult to suggest something further without being able to run your code. Can you attach the data you have and the functions used in the above code? Use the paperclip button to attach the files.
Matt R
on 2 Aug 2023
Dyuman Joshi
on 2 Aug 2023
"I have pre-allocated my variables according to the below snippet."
That is not the kind of preallocation that will help you. Go through the link I shared.
"I have noticed though that for P_evap_TD, P_TD etc above, they still give the same error when inside the For loop."
What error do you get? Please copy and paste the whole error (all of the red text)
Also, I believe you have not attached the function Depth()
D = Depth(R2,Tp(k),Y2,FR,MC);
%Command just above the last for loop
Cris LaPierre
on 2 Aug 2023
@Matt R It's not that for loops are slow in MATLAB. It's that, in MATLAB, you can often vectorize your loops, which is often faster than for loops. The speed up is from running a line of code once instead of N times.
See here for more: https://www.mathworks.com/help/matlab/matlab_prog/vectorization.html
Matt R
on 3 Aug 2023
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.
Answers (1)
Sai Teja G
on 23 Aug 2023
0 votes
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!
Categories
Find more on MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!