execution of code very long

조회 수: 7 (최근 30일)
Fatiha
Fatiha 2012년 5월 23일
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
Friedrich
Friedrich 2012년 5월 23일
Please format you code to make it readable. Thanks :)

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

답변 (1개)

Daniel Shub
Daniel Shub 2012년 5월 23일
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?

카테고리

Help CenterFile Exchange에서 Audio Processing Algorithm Design에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by