execution of code very long

7 views (last 30 days)
Fatiha
Fatiha on 23 May 2012
Hi, I need help to improve the following code. The execution is too long 79.014s
function [Dmin]=dist_am(Q,R,Obs,N,d,n_obs,contact,comp)
% Dp is matrix of distances
% attention D(c) corresponds to c ieme contact possible
% Dmin= min(D)
Dmin=0;
if comp>0 % table of contact is not empty
Dp=zeros(comp,1);
% on parcourt le tableau de contact
for c=1:comp
if(contact(c,3)==0) % contact particule-particule
i=contact(c,1);
j=contact(c,2);
p=norm(Q(:,j)-Q(:,i));
Dp(c)=p-(R(i)+R(j));
else
% contact obstacle-particule
i=contact(c,1);
l=contact(c,3);
% zone ou se trouve la part i par rapport a l'obst l
if (Q(:,i)-Obs(1:2,l))'*(Obs(1:2,l)-Obs(3:4,l))>0
% la part est dans la zone 1
p=norm(Q(:,i)-Obs(1:2,l));
Dp(c)=p-R(i);
elseif (Q(:,i)-Obs(3:4,l))'*(Obs(3:4,l)-Obs(1:2,l))>0
% la part est dans la zone 2
p=norm(Q(:,i)-Obs(3:4,l));
Dp(c)=p-R(i);
else
pro_scal=(Q(:,i)-Obs(1:2,l))'*(N(:,l));
if pro_scal>0
% la part est dans la zone 3
Dp(c)=pro_scal-R(i);
else
% la part est dans la zone 4
Dp(c)=-pro_scal-R(i);
end
end
end
end
Dmin=min(Dp);
end
Thank you.
  1 Comment
Friedrich
Friedrich on 23 May 2012
Please format you code to make it readable. Thanks :)

Sign in to comment.

Answers (1)

Daniel Shub
Daniel Shub on 23 May 2012
Have you tried to profile your code to figure out where the bottleneck is? If comp is really big, you might be able to get a little speed up by not constructing the entire Dp matrix. You could do the comparison on the fly and only need the current Dp and Dmin.
Depending on what contact looks like, you might be recomputing components. The only dependency on c in your loop is in the construction of i and j (and i and l). If you do the calculations for individual pairs multiple times, that will slow you down.
Have you thought about a parfor or using the PCT?

Categories

Find more on Audio Processing Algorithm Design in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!