How can I make this code more efficient?

조회 수: 1 (최근 30일)
Pooneh Shah Malekpoor
Pooneh Shah Malekpoor 2023년 6월 27일
댓글: Stephen23 2023년 6월 27일
Hi
This piece of code is so slow due to the big number of nx1 and nye. Could you please tell me how I can make it quicker?
Thanks
nx1=1216;
nye=160;
dx=1.973684;
dy=1.875;
nm=0;
for ip=1:nx1
for iq=1:nye
nm=nm+1;
coord(1, 1) = (ip - 1) * dx;
coord(2, 1) = (ip - 1) * dx;
coord(3, 1) = (ip - 1) * dx;
coord(5, 1) = ip * dx;
coord(6, 1) = ip * dx;
coord(7, 1) = ip * dx;
coord(4, 1) = (coord(3, 1) + coord(5, 1)) * 0.5;
coord(8, 1) = coord(4, 1);
coord(3, 2) = -(iq - 1) * dy;
coord(4, 2) = -(iq - 1) * dy;
coord(5, 2) = -(iq - 1) * dy;
coord(1, 2) = -iq * dy;
coord(8, 2) = -iq * dy;
coord(7, 2) = -iq * dy;
coord(2, 2) = (coord(1, 2) + coord(3, 2)) * 0.5;
coord(6, 2) = coord(2, 2);
for i=1:8
g_coordx(nm, i) = coord(i, 1);
g_coordy(nm, i) = coord(i, 2);
end
end
end

채택된 답변

KSSV
KSSV 2023년 6월 27일
Read abput preallocating/ initializing the variables.
nx1=1216;
nye=160;
dx=1.973684;
dy=1.875;
nm=0;
t1 = tic ;
% See this
g_coordx = zeros(nx1*nye,8) ;
g_coordy = zeros(nx1*nye,8) ;
%
for ip=1:nx1
fprintf('% d of %d\n',ip,nx1) ;
for iq=1:nye
nm=nm+1;
coord(1, 1) = (ip - 1) * dx;
coord(2, 1) = (ip - 1) * dx;
coord(3, 1) = (ip - 1) * dx;
coord(5, 1) = ip * dx;
coord(6, 1) = ip * dx;
coord(7, 1) = ip * dx;
coord(4, 1) = (coord(3, 1) + coord(5, 1)) * 0.5;
coord(8, 1) = coord(4, 1);
coord(3, 2) = -(iq - 1) * dy;
coord(4, 2) = -(iq - 1) * dy;
coord(5, 2) = -(iq - 1) * dy;
coord(1, 2) = -iq * dy;
coord(8, 2) = -iq * dy;
coord(7, 2) = -iq * dy;
coord(2, 2) = (coord(1, 2) + coord(3, 2)) * 0.5;
coord(6, 2) = coord(2, 2);
for i=1:8
g_coordx(nm, i) = coord(i, 1);
g_coordy(nm, i) = coord(i, 2);
end
end
end
t1 = toc(t1) ;

추가 답변 (1개)

Swastik Sarkar
Swastik Sarkar 2023년 6월 27일
편집: Swastik Sarkar 2023년 6월 27일
  • Assuming you have not preallocated the variables g_coordx & g_coordy, you can consider preallocating them to improve time and performance of the code. Read about it here
  • The first part of the loops only depend on variable ip, so you can write it out of the loop
So the final code is:
nx1=1216;
nye=160;
dx=1.973684;
dy=1.875;
nm=0;
g_coordx = zeros(nx1*nye, 8);
g_coordy = zeros(nx1*nye, 8);
coord = zeros(8, 2);
for ip = 1:nx1
coord(1, 1) = (ip - 1) * dx;
coord(2, 1) = (ip - 1) * dx;
coord(3, 1) = (ip - 1) * dx;
coord(5, 1) = ip * dx;
coord(6, 1) = ip * dx;
coord(7, 1) = ip * dx;
coord(4, 1) = (coord(3, 1) + coord(5, 1)) * 0.5;
coord(8, 1) = coord(4, 1);
for iq = 1:nye
nm = (ip - 1) * nye + iq; % In case you need nm later when you modify it
coord(3, 2) = -(iq - 1) * dy;
coord(4, 2) = -(iq - 1) * dy;
coord(5, 2) = -(iq - 1) * dy;
coord(1, 2) = -iq * dy;
coord(8, 2) = -iq * dy;
coord(7, 2) = -iq * dy;
coord(2, 2) = (coord(1, 2) + coord(3, 2)) * 0.5;
coord(6, 2) = coord(2, 2);
g_coordx(nm, :) = coord(:, 1)';
g_coordy(nm, :) = coord(:, 2)';
end
end
You can also go through Techniques to improve performance

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by