Why is MATLAB running slow? 2013a vs 2011a
1 回表示 (過去 30 日間)
古いコメントを表示
Hi everyone,
I am running an m-file in Matlab 2013a, which takes roughly 20sec to complete. However, when a friend runs the same code in 2011a it takes a fraction of a second to complete. What could be causing this in my 2013a installation?
Our PCs have the same components so that is not the culprit.
Just a note; the code I am testing is very simple, for the most part just for loops.
Thanks
clc; clear all; close all;
format compact
% Define number of cells in x and y
nx = 20;
ny = 20;
uw = 1;
% Define cell length in x and y
dx = 1.0/nx;
dy = 1.0/ny;
% Relaxation Parametres
relax1 = 0.2;
relax2 = 1.2;
relax3 = 0.2;
% Reynolds Number
re = 100;
nu = 1/re;
kappa = 1/re;
rex = 2.0/dx/dx + 2.0/dy/dy;
% Initialise the streamfunction and vorticity everywhere.
for i = 1:nx+1
for j = 1:ny+1
psi(i,j) = 0.0; % Streamfunction
w(i,j) = 0.0;
x(i,j) = (i-1)*dx;
y(i,j) = (j-1)*dy;
T(i,j) = 0.0;
if j == ny+1
T(i,j) = 1.0;
end
end;
end;
for k = 1:20000 %main loop - iterate until convergence.
adu = 0.0;
adv = 0.0;
psimin = 1000000.0;
psimax = -1000000.0;
for i = 2:nx %sweep through all internal points on the grid
for j = 2:ny %updating until convergence.
wimj0 = w(i-1,j); %for the vorticity equations these values
wipj0 = w(i+1,j); %may have to be modified at the points
wi0jm = w(i,j-1); %adjacent to the walls
wi0jp = w(i,j+1);
% Boundary Adjacent Conditions
if(i == 2)
wimj0 = -2.0*psi(i,j)/dx/dx;
end;
if(i == nx)
wipj0 = -2.0*psi(i,j)/dx/dx;
end;
if(j == 2)
wi0jm = -2.0*psi(i,j)/dy/dy;
end;
if(j == ny)
wi0jp = -2.0*(psi(i,j)+dy*uw)/dy/dy;
end;
%this is the update for w(i,j) from the FDE.
wij = (nu*((wipj0+wimj0)/dx/dx + (wi0jp+wi0jm)/dy/dy)...
+ (((psi(i+1,j)-psi(i-1,j))*(wi0jp-wi0jm)-((psi(i,j+1)...
-psi(i,j-1))*(wipj0-wimj0)))/(4*dx*dy)))/(nu*rex);
dw = wij - w(i,j);
%compute the difference between new and old value.
w(i,j) = w(i,j) + relax1*dw;
%update using relaxation
%update equation for the streamfunction
psiij = ((psi(i+1,j)+psi(i-1,j))/dx/dx + ...
(psi(i,j+1)+psi(i,j-1))/dy/dy + w(i,j))/rex;
dpsi = psiij - psi(i,j);
psi(i,j) = psi(i,j) + relax2*dpsi;
if(psi(i,j)>psimax) %determine maximum and minimum streamfunction
psimax = psi(i,j);
end;
if(psi(i,j)<psimin)
psimin = psi(i,j);
end;
ddu = abs(dpsi); %compute maximum change in solution over full sweep.
ddv = abs(dw);
if(ddu>adu)
adu = ddu;
end;
if(ddv>adv)
adv = ddv;
end;
end;
end;
% k
% psimax
% psimin
% adu
% adv
% Exit loop if converged
if(adu<1.0e-4 && adv<1.0e-4)
break;
end;
end;
% Compute the temperature distribution
for l = 1:10000
adT = 0.0;
for i = 1:nx+1
for j = 1:ny+1
if j ~= 1 && j ~= ny+1
if i == 1 % Using a forward difference scheme on the psi differential
Tij = (kappa*((T(i,j+1)+T(i,j-1))/dy/dy) + ...
(psi(i+1,j)-psi(i,j))*(T(i,j+1)-T(i,j-1))/(2*dx*dy))/(kappa*2/dy/dy);
elseif i == nx+1 % Using a backward difference scheme on the psi differential
Tij = (kappa*((T(i,j+1)+T(i,j-1))/dy/dy) + ...
(psi(i,j)-psi(i-1,j))*(T(i,j+1)-T(i,j-1))/(2*dx*dy))/(kappa*2/dy/dy);
else
Tij = (kappa*((T(i+1,j)+T(i-1,j))/dx/dx + ...
(T(i,j+1)+T(i,j-1))/dy/dy) + ...
(psi(i+1,j)-psi(i-1,j))*(T(i,j+1)-T(i,j-1))/(4*dx*dy)...
- (psi(i,j+1)-psi(i,j-1))*(T(i+1,j)-T(i-1,j))/(4*dx*dy))/(kappa*rex);
end;
dT = Tij - T(i,j);
T(i,j) = T(i,j) + dT*relax3;
ddT = abs(dT);
if(ddT>adT)
adT = ddT;
end;
end;
end;
end;
if adT < 1e-6
break
end
% l
% adT
end;
k
psimin
adu
adv
l
adT
% Plot solution.
for i = 1:10
v(i) = -(i-1)*0.01;
end;
for i = 11:20
v(i) = (i-10)*psimax/10;
end;
figure(1)
contourf(x,y,psi,v);
xlabel('x')
ylabel('y')
title('Streamfunction')
colorbar
figure(2)
contourf(x,y,T)
xlabel('x')
ylabel('y')
title('Temperature Distribution (\circC)')
colorbar
2 件のコメント
採用された回答
Edric Ellis
2014 年 10 月 7 日
I tried your code on my machine in both R2011a and R2014a, and as posted it is indeed quite a bit slower in R2014a. You can get nearly all the speed back if you make your code a function rather than a script. See this page for more about functions if you're not already used to writing them.
4 件のコメント
Edric Ellis
2014 年 10 月 7 日
I'm afraid I'm not an expert on the interpretation of the MATLAB language, but I believe basically you're on the right lines that there are a bunch of optimisations that can be applied inside a function because of things like the reduced variable scope.
その他の回答 (2 件)
Andreas Goser
2014 年 10 月 7 日
When I copy / paste the code, I need to correct too many smaller issues as of unintended line breaks that I can be sure I run the code you are using. A couple of comments:
I understand you say your machines have "the same components". I can however share that the majority of such effects is happening not from release to relase, but when changing hardware. I suggest you do an R2011a/b installation on your machine in parallel to narrow down the issue. Even then, there can be issues with stuff like the math kernel library.
Second, I wonder if your question is really finding out the why or if you need to reduce runtime. There might be opportunities to get rid of loops.
2 件のコメント
Andreas Goser
2014 年 10 月 7 日
Memory should not be an issue - all matrices involed appear to be small.
What I wonder if - as you do iterations - the code breaks out of the loop at different times?
yonatan gerufi
2014 年 10 月 6 日
there are many reason this could happen.
any other programs are running?
try
memory
to see how much matlab use, and also do
clear all
perhaps it will help.
if it doesn't help you can use "profiler" to see your time for each line.
you can upload the code, so other people will compare there time to yours.
参考
カテゴリ
Help Center および File Exchange で Startup and Shutdown についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!