How to reduce the time complexity?

1 visualizzazione (ultimi 30 giorni)
.. Vamshi
.. Vamshi il 5 Feb 2013
The following code is taking much time to execute. What I wanted to do is.. Dividing the matrix of size 90x90 into 9x9 grids and calculating the angle counts in each range. Its taking a lot of time for computation. But I need to reduce this at any cost. Could not find the solution.
fsrc = sprintf('FeatureVector%d',ii);
%FeatureVector=zeros([100,8]);
cnt1=0; cnt2=0; cnt3=0; cnt4=0; cnt5=0; cnt6=0; cnt7=0; cnt8=0;
p = 1;
row = 1;
rlength = size(Gdir,1);
clength = size(Gdir,2);
while p<(rlength)
q = 1;
while q<(clength)
for i=p:p+8
for j=q:q+8
if Gdir(i,j)>=-180 && Gdir(i,j)<=-134
cnt1 = cnt1+1;
elseif Gdir(i,j)>=-135 && Gdir(i,j)<=-89
cnt2 = cnt2 + 1;
elseif Gdir(i,j)>=-90 && Gdir(i,j)<=-44
cnt3 = cnt3 + 1;
elseif Gdir(i,j)>=-45 && Gdir(i,j)<=-1
cnt4 = cnt4 + 1;
elseif Gdir(i,j)>=0 && Gdir(i,j)<=45
cnt5 = cnt5 + 1;
elseif Gdir(i,j)>=46 && Gdir(i,j)<=90
cnt6 = cnt6 + 1;
elseif Gdir(i,j)>=91 && Gdir(i,j)<=135
cnt7 = cnt7 + 1;
else
cnt8 = cnt8 + 1;
end
eval([fsrc '(row,1) = cnt1;']);
eval([fsrc '(row,2) = cnt2;']);
eval([fsrc '(row,3) = cnt3;']);
eval([fsrc '(row,4) = cnt4;']);
eval([fsrc '(row,5) = cnt5;']);
eval([fsrc '(row,6) = cnt6;']);
eval([fsrc '(row,7) = cnt7;']);
eval([fsrc '(row,8) = cnt8;']);
end
end
q = q + 9; row = row+1;
cnt1=0; cnt2=0; cnt3=0; cnt4=0; cnt5=0; cnt6=0; cnt7=0; cnt8=0;
end
p = p + 9;
end
dst = sprintf('FV%d.mat',ii);
s = ['save ./FeatureVectors/' dst ' ' fsrc ';'];
eval(s);

Risposta accettata

Sean de Wolski
Sean de Wolski il 5 Feb 2013
Well first off, don't use eval(); it's evil and slow.
Second, considering that row never changes, you don't need to icncrement row each time. But then again this is what I would do instead of using eval and cnt1:cnt5
if Gdir(i,j)>=-180 && Gdir(i,j)<=-134
fsrc(row,1) = fsrc(row,1)+1;
elseif Gdir(i,j)>=-135 && Gdir(i,j)<=-89
etc.
Also, it looks like you're just doing a histogram:
fsrc = histc(Gdir(:),[-180 -134 -89 etc.])
And, most importantly, use the profiler to profile your code and identify bottlenecks.
  1 Commento
.. Vamshi
.. Vamshi il 6 Feb 2013
Oh..Ya Thanks alot. I'll try the suggested changes.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Numerical Integration and Differential Equations in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by