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!