How to increase the speed of a FOR loop?
1 次查看(过去 30 天)
显示 更早的评论
Hi, I have a problem with few lines of a for loop. It seems that it takes forever to finish. After one hour of waiting the for loop wasn't executed to the end, so I interrupted it manually. Also some variables within the loop are to big in size, one of them is class double 558082904 bytes and one is struct array of 4646641536 bytes in size. After this loop finishes I should have one 20x1 struct arrays with 4 fields. Each field is a matrix 33x20001x11. The other struct array is 20x1 with one field that is also 33x20001x11.
This is the code
for p = 1:nPop
for i=2:n
for j = 1:length(beam_transverse);
%Effective loads
EffectiveLoad(p).Pe(:,i,j) = ImpactForce(:,i-1) + theta * ...
(ImpactForce(:,i)-ImpactForce(:,i-1)) + MASS_EB_W *...
(a0*ResponseVar(p).displ(:,i-1,j) + a2*ResponseVar(p).vel(:,i-1,j)...
+ 2*ResponseVar(p).acc(:,i-1,j)) + DAMPING_PSO(p).DAMPING_EBW_SD*(a1*...
ResponseVar(p).displ(:,i-1,j) + 2*ResponseVar(p).vel(:,i-1,j) + ...
a3*ResponseVar(p).acc(:,i-1,j));
ResponseVar(p).dispth(:,i,j)=EffectiveStiff(p).Ke\EffectiveLoad(p).Pe(:,i,j);
%Calculate the displacements, velocities and accelerations at time t+dt.
%Calculate the reponse for every hammer impact
%Accelerations
ResponseVar(p).acc(:,i,j) = a4*(ResponseVar(p).dispth(:,i,j) - ResponseVar(p).displ(:,i-1,j)) + ...
a5*ResponseVar(p).vel(:,i-1,j) + a6*ResponseVar(p).acc(:,i-1,j);
%Velocity
ResponseVar(p).vel(:,i,j) = ResponseVar(p).vel(:,i-1,j) + a7*(ResponseVar(p).acc(:,i,j)...
+ ResponseVar(p).acc(:,i-1,j));
%Displacements
ResponseVar(p).displ(:,i,j) = ResponseVar(p).displ(:,i-1,j) + dt*...
ResponseVar(p).vel(:,i-1,j) + a8*(ResponseVar(p).acc(:,i,j) + 2*ResponseVar(p).acc(:,i-1,j));
end
end
end
nPop is 20, n is 20001 and j is 11.
I am not sure that this loop is the best way of formulating my problem. Do you have any suggestions to formulate this in a different way to avoid the indefinite time for calculation to finish?
Thanks in advance!
1 个评论
Stephen23
2017-2-6
Most likely array preallocation would make a very significant difference to the calculation time:
回答(1 个)
Ayush
2017-3-27
load program onto graphics card, it will certainly reduce computation time.
These link might help you
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!