Matlab computation time is very high for first run
4 ビュー (過去 30 日間)
古いコメントを表示
Hello all, In the attached Matlab code there is one issue. If I run this code first time then computation time is in the range of 0.055 secs. But when I run the same code subsequent times (i.e. 2nd and 3rd times..) then computation time is around 0.015 secs. Can anybody please tell me, why Matlab computation time is so high in first run? I have also attached plot for computation time as a function of number of runs. Thank you in advance for your help,
Sincerely, Nik
Code:
clear all;
for t=1:1000
tic
l=10;
k=5;
a=-1;
b=1;
hh=(b-a)/(2^l);
H=(2^k)*hh;
ll=a-2*H;
ul=b+2*H;
x=a-2*H:hh:b+2*H;
y=x;
pH=cell(k+1,1);
pX=cell(k+1,1);
pY=cell(k+1,1);
for i=1:1:k+1
H=(2^(i-1))*hh;
pH{i,1}=H;
xX=ll:H:ul;
xY=xX;
pX{i,1}=xX;
pY{i,1}=xY;
end
nx=2^(l)+1;
u=zeros(length(y),1);
u(1+2^(k+1):end-2^(k+1))=(1*ones(1,nx)-(y(1+2^(k+1):end-2^(k+1)).^2));%sin(y(1+2^(k+1):end-2^(k+1))).^2;
pU=cell(k+1,1);
pW=cell(k+1,1);
pRhH=cell(k,1);
pK=cell(k+1,1);
pU{1}=u;
m=length(x);
pN{1,1}=m;
RhH=zeros((m+1)/2,m);
RhH(1,1:4)=(1/16)*[16 9 0 -1];
RhH(2,1:6)=(1/16)*[0 9 16 9 0 -1];
RhH((m+1)/2-1,end-5:end)=(1/16)*[-1 0 9 16 9 0];
RhH((m+1)/2,end-3:end)=(1/16)*[-1 0 9 16];
%tic
for i=3:1:(m+1)/2-2
RhH(i,2*i-4:2*i+2)=(1/16)*[-1 0 9 16 9 0 -1];
end
pRhH{1}=RhH;
for i=1:1:k
pU{i+1,1}=(1/2)*pRhH{i}*pU{i};
rhH=pRhH{i};
[n,m]=size(rhH);
pN{i+1,1}=n;
pRhH{i+1}=rhH(1:(length(pU{i+1})+1)/2,1:length(pU{i+1}));
end
n=length(pU{k+1});
K4=zeros(n,n);
W=zeros(n,1);
U=pU{k+1};
%
nm=pN{1};
DD=zeros(1,nm+1);
% Tt=ones(nm,1);
% Ksuper=zeros(nm,nm);
for i=1:1:pN{1}
c=hh/2;d=-hh/2;
D1= log(sqrt(((i-1)*hh-c)^2 + 2) + (i-1)*hh - c);
D2= log(sqrt(((i-1)*hh-d)^2 + 2) + (i-1)*hh - d);
DD(1,i)=-D1+D2;
end
%Perform actual integration on coarse grid
for I=1:1:n
for J=1:1:n
i=(2^k)*(I-1)+1;
j=(2^k)*(J-1)+1;
K4(I,J)=DD(abs(i-j)+1);
W(I)=W(I)+(H/hh)*K4(I,J)*U(J);
end
end
pW{k+1,1}=W;
pK{k+1}=K4;
for i=k:-1:1
T=int32(2^i);
TT=T/2;
n=pN{i,1};%n is the number of points on fine grid on which we will interpolate now
m=int32(3+1*log(n));
m=round(m);
kl=round(m/2)+3;
W=pW{i+1,1};
IHh=pRhH{i,1}';
X=pX{i+1,1};
x=pX{i,1};
Y=pY{i+1,1};
y=pY{i,1};
u=pU{i,1};
h=pH{i,1};
N=pN{i+1};
%Retrieve Kernel K on coarse mesh
KNN=pK{i+1};
pm=length(KNN);
if i==k
v3=KNN((1+N)/2,-kl+(1+N)/2:kl+(1+N)/2)';
else
v3=KNN(-kl+(1+pm)/2:kl+(1+pm)/2);
end
r=2*(2*kl+1)-1;
Khat1=IHh(1:r,1:2*kl+1)*v3;
% Khat1=IHh*v3;
pK{i,1}=Khat1;
%Kprime1=Khat1(-m+(n+1)/2:m+(n+1)/2);
Kprime1=Khat1(-m+(r+1)/2:m+(r+1)/2);
K7=repmat(Kprime1,1,N);
mm=(-m:1:m)';
mmm=TT*abs(mm)+1;
mM=repmat(mmm,1,N);
K99=DD(mM);
M=repmat(mm,1,n);
JJ=int32(1:1:n);%2*JJ-1;
JJ=repmat(JJ,2*m+1,1);
JM=(JJ+M);
u(n+1,1)=0;
JM(JM<=0)=n+1;
JM(JM>n)=n+1;
U=u(JM);
U1=U(:,1:2:end);
U2=U(:,2:2:end-1)';
corr=(K99-K7)'*U1;
Correction=diag(corr);
W=W+Correction*h/hh;
w=IHh*W;
K88=K99(:,1:end-1)';
v4=TT*((n+1)/2);
v4=v4*int32(ones(N,1));
v5=int32(1:1:N)';
v5=T*(v5-1);
v6=DD(abs(v4-v5)+1);
v7=IHh*v6';
v8=v7(-m+(n+1)/2+1:m+(n+1)/2+1)';
K10=repmat(v8,(n-1)/2,1);
corr4=(K88-K10)*U2';
corr3=diag(corr4);
w(2:2:end-1)=w(2:2:end-1)+corr3*(h/hh);
pW{i,1}=w;
end
wfine=pW{1,1};
et(t)=toc;
end
mean(et)
0 件のコメント
回答 (1 件)
Walter Roberson
2018 年 3 月 26 日
Code can be slower the first time it executes because MATLAB uses a Just In Time Compiler that has to execute the first time. After the first time a line is executed then MATLAB has internal data structures that are faster to execute than the first time it looks at them.
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!