How can speed up the blow codes?

조회 수: 1 (최근 30일)
Amir Torabi
Amir Torabi 2019년 12월 2일
편집: Fabio Freschi 2019년 12월 3일
The blow code is part of my project. the performance of this code is alittle low. it is about 0,5s but in higher Nx values, the effiency decreases. Also, etas is a (Nx*Ny,ngrain=70) matrix. it does not contain zero values. I'd be appreciate if any suggest can improve the efficiency of below code.
Nx=512;
for igrain=1:70+0
ncount=0;
for i=1:Nx
for j=1:Nx
ii =(i-1)*Nx+j;
eta2(i,j) =eta2(i,j)+etas(ii,igrain)^2;
if(etas(ii,igrain) >= 0.5)
ncount=ncount+1;
end
ncount=ncount/(512*512);
end
end
  댓글 수: 1
Nicolas B.
Nicolas B. 2019년 12월 2일
편집: Nicolas B. 2019년 12월 2일
are eta2 and etas pre-defined tables? Or are the size of these variables change?

댓글을 달려면 로그인하십시오.

답변 (1개)

Fabio Freschi
Fabio Freschi 2019년 12월 2일
편집: Fabio Freschi 2019년 12월 2일
Edit: added attempt for ncount
I don't understand what ncount is doing, it looks like a counter but I don't understand why you are repeatedly dividing by 512*512. I made my try, but some insight could help. However the calculation of eta2 can be drastically simplified using vectorization. Here I compare your solution (with some fixes) with the vectorized version (with a '0' added o the variables' names)
Nx = 512;
ngrain = 70;
eta2 = zeros(Nx,Nx);
etas = rand(Nx*Nx,ngrain);
ncountTot = zeros(ngrain,1);
% original code (with some fixes)
tic
for igrain=1:ngrain
ncount=0;
for i=1:Nx
for j=1:Nx
ii =(i-1)*Nx+j;
eta2(i,j) =eta2(i,j)+etas(ii,igrain)^2;
if(etas(ii,igrain) >= 0.5)
ncount=ncount+1;
end
end
ncountTot(igrain)=ncount/(Nx*Nx);
end
end
toc
% vectorized code
tic
etas0 = reshape(etas,Nx,Nx,ngrain);
eta20 = sum(etas0.^2,3)';
ncountTot0 = squeeze(sum(etas0 >= 0.5,[1 2])/(Nx*Nx));
toc
% check
norm(eta2-eta20)/norm(eta2)
norm(ncountTot-ncountTot0)
  댓글 수: 2
Amir Torabi
Amir Torabi 2019년 12월 3일
Thanks Fabio. it works great. could you ask you have your advice in the improvment of running time of following code?
term3=0;
ngrian=70;
glist = round(rand(70,1));
etas = rand(512*512,70);
den = zeros(70,70);
en = en = rand(1,70)*10;
for jgr=1:ngrain
if(glist(jgr)== 1)
den(igrain,jgr)=en(jgr)-en(igrain);
term3=term3-8/pi*(etas(:,igrain).*etas(:,jgr)).^0.5*den(igrain,jgr);
end
end
%end
Fabio Freschi
Fabio Freschi 2019년 12월 3일
편집: Fabio Freschi 2019년 12월 3일
My pleasure! If the answer solves your original problem, please accept it.
For the second code, could you please check it? it looks like one for loop is missing
Do you need at the end of the loop only term3 or also den?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Function Creation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by