Reduce the computational time

1 visualización (últimos 30 días)
Nkenna Amadi
Nkenna Amadi el 13 de Jun. de 2018
Comentada: Jan el 14 de Jun. de 2018
Hello, i have this code as follows below, it currently takes me about 20mins to get results, is there anyhow i can reduce computational time to under 5 mins?
global Lx;
global Ly;
Lx = 80; % Defines the size of the lattice .
Ly = 200;
pt =rand(200,80);
global IM;
IM = ones(Ly,Lx).*2; % Initialization of the lattice
% IM( i , j )=2
t =1;
tend =1; % Number of "time steps".
k = 1; % Counting index for front propagation .
q = 1; % Counting index for image writing .
% Initial step. The invader starts from a "point" or a channel
%IM(Ly,50)=1; % Point IP
IM(Ly,1:Lx)=1; % Channel IP
% Finds the indices of the largest element in a given row in pt , % and invades (=1) the corresponding element in IM
IM(Ly-1,find(pt(Ly-1,1:Lx)==max(pt(Ly-1,1:Lx)))) = 1;
while tend==1
if max(IM(2,1:end))==1
break
end
% Check if there are trapped clusters
IM=bwlabel(IM-1,4); % Labels defending clusters , by setting invader sites = 0.
IM=IM+1; % Keeps the labeling , and puts the invader sites = 1.
for i = 1:Ly
s = sort(IM(i,1:end)); % Sorts the i?th row in ascending order.
if s(1)==1 % Tests if the i ? th row contains an invader site .
for j = 1:Lx
bin = front(i-1,j); % If the i?th row contains an invader site , all the elements in the (i?1)?th row are % being tested in the function front .m to % see if they are part of the front .
if bin==1
% pfront is the front matrix where the first column
% contains the probability and the second and third
% columns contain the corresponding indices for this front %site.
pfront(k,1) = pt(i-1,j );
pfront(k,2) = i-1;
pfront(k,3) = j;
k = k+1000;
end
end
end
end
% Probability . % y?coordinate. %x?coordinate.
% Finds the row?index of the largest probability in pfront , % and invades (=1) the corresponding element in IM
e = find(pfront(1:end,1)==max(pfront(1:end,1))); IM(pfront(e,2),pfront(e,3)) = 1;
clear pfront
k = 1;
% Put invader sites = 1, and defending sites = 0.
for i = 1:Ly
for j = 1:Lx
if IM(i,j)~=1
IM ( i , j ) = 0 ;
end
end
end
% The following lines saves the image for each iteration .
if (( t/100)==q)
pic = label2rgb(IM(2:end,1:end)); number = int2str(q);
picloc = strcat('f:\',number,'.tif'); imwrite ( pic , picloc ,'tif') ;
q = q+1
end
t = t+1
end
%sub-function run on a seperate matlab file to be named "front.m"
function [bin] = front(a,b)
global IM
global Lx
global Ly
% The site is part of the front if one of the neighboring sites % is an invader.
if IM(a,b)~=IM(1,1) % Takes trapped clusters into account . If the site
% tested is not part of the large defending cluster it
% should not be counted as a front site .
bin=0;
elseif (b+1)==(Lx+1)
if (IM(a+1,b)==1)||(IM(a-1,b)==1)||(IM(a,b-1)==1) bin = 1;
else
bin = 0;
end
elseif (b-1)==(1-1)
if (IM(a+1,b)==1)||(IM(a-1,b)==1)||(IM(a,b+1)==1) bin = 1;
else
bin = 0;
end
elseif (a+1)==(Ly+1)
if (IM(a-1,b)==1)||(IM(a,b+1)==1)||(IM(a,b-1)==1) bin = 1;
else
bin = 0;
end
elseif (a-1)==(1-1)
if (IM(a+1,b)==1)||(IM(a,b+1)==1)||(IM(a,b-1)==1) bin = 1;
else
bin = 0;
end
else
if (IM(a+1,b)==1)||(IM(a-1,b)==1)||(IM(a,b+1)==1)||(IM(a,b-1)==1) bin = 1;
else
bin = 0;
end
end
  2 comentarios
Geoff Hayes
Geoff Hayes el 14 de Jun. de 2018
Nkenna - you may want to try the profile tool to get an idea of where your code is spending much of its time. I couldn't get your code (problem with pfront) to run so please confirm that the above is valid. (Try formatting it to ensure that (to you) the code looks to be doing what it should.)
Nkenna Amadi
Nkenna Amadi el 14 de Jun. de 2018
hi yes the function works. you just have the save the subfunction as a seperate m file called "front", then on a seperate file run the first part of the code(without the subfunction part). Hope thats a bit clearer. I've attached the subfunction code again below. Ive tried using the profiling tool and its told me what takes the most time but im not quite sure how to solve this. The one thing it tells me though is that "pfont" appears to change size on every iteration and that i should consider preallocating for speed.
%sub-function run on a seperate matlab file to be named "front.m"
function [bin] = front(a,b)
global IM
global Lx
global Ly
% The site is part of the front if one of the neighboring sites % is an invader.
if IM(a,b)~=IM(1,1) % Takes trapped clusters into account . If the site
% tested is not part of the large defending cluster it
% should not be counted as a front site .
bin=0;
elseif (b+1)==(Lx+1)
if (IM(a+1,b)==1)||(IM(a-1,b)==1)||(IM(a,b-1)==1) bin = 1;
else
bin = 0;
end
elseif (b-1)==(1-1)
if (IM(a+1,b)==1)||(IM(a-1,b)==1)||(IM(a,b+1)==1) bin = 1;
else
bin = 0;
end
elseif (a+1)==(Ly+1)
if (IM(a-1,b)==1)||(IM(a,b+1)==1)||(IM(a,b-1)==1) bin = 1;
else
bin = 0;
end
elseif (a-1)==(1-1)
if (IM(a+1,b)==1)||(IM(a,b+1)==1)||(IM(a,b-1)==1) bin = 1;
else
bin = 0;
end
else
if (IM(a+1,b)==1)||(IM(a-1,b)==1)||(IM(a,b+1)==1)||(IM(a,b-1)==1) bin = 1;
else
bin = 0;
end
end

Iniciar sesión para comentar.

Respuesta aceptada

Jan
Jan el 14 de Jun. de 2018
Just one of the many points:
for i = 1:Ly
for j = 1:Lx
if IM(i,j)~=1
IM ( i , j ) = 0 ;
end
end
end
by
IM(IM ~= 1) = 0;
  2 comentarios
Nkenna Amadi
Nkenna Amadi el 14 de Jun. de 2018
thank you, what else could i improve on? from the profile analysis i can see that using "global" takes alot of processing time? is there a way around this ?
Jan
Jan el 14 de Jun. de 2018
  • Globals are a bad design in every case, but most of all because the time required for debugging can explode. Provide the data as input arguments.
  • elseif (a-1)==(1-1)
This will not need much run time, but
elseif a == 1
is nicer. Same for elseif (a+1)==(Ly+1): esleif a==Ly.
  • Use one command per line only.
  • pfront(:,1) is faster than pfront(1:end,1).
  • Omit the find in
IM(Ly-1,find(pt(Ly-1,1:Lx)==max(pt(Ly-1,1:Lx)))) = 1;
(This is marked in the editor as hint already)
*

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Line Plots en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by