Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Problem with the time-efficiency of the code

Subject: Problem with the time-efficiency of the code

From: Liana

Date: 9 Sep, 2011 21:15:28

Message: 1 of 3

Hello,

I would like to improve the time efficiency of my code (see below):

//========================================
//There is a structure "s" consisted of delaunay triangulations of different geometrical objects.
s = struct('tess', {});
<<...>>
//E.g.
s(1).tess = DelaunayTri(v(:,1),v(:,2),v(:,3));

// This function veryfies the intersection of all faces of tetrahedrons with the curve
function answer = f1(...);
tessqty = length(s(:));
P1 = []; P2 = []; P3 = [];
for i = 1 : tessqty
   tess = s(i).tess;
   faces = [tess(:,[1 2 3]); tess(:,[2 3 4]); ...
            tess(:,[1 3 4]); tess(:,[1 2 4])];
   P1 = [P1; s(i).tess.X(faces(:,1),1) s(i).tess.X(faces(:,1),2) s(i).tess.X(faces(:,1),3)];
   P2 = [P2; s(i).tess.X(faces(:,2),1) s(i).tess.X(faces(:,2),2) s(i).tess.X(faces(:,2),3)];
   P3 = [P3; s(i).tess.X(faces(:,3),1) s(i).tess.X(faces(:,3),2) s(i).tess.X(faces(:,3),3)];
end
walk = ones(leng,1);
for j = 1 : leng
    for i = 1 : length(P1(:,1))
        [flag,u,v,a] = rayTriangleIntersection(origin(j,:),direct(j,:),P1(i,:),P2(i,:),P3(i,:));
            if flag == 1
                intersection = origin(j,:) + a*direct(j,:);
                if pointSegment(intersection,origin(j,:),dest(j,:))
                    walk(j) = 0;
                    break,
                end
            end;
    end;
end;
answer = walk;
end;
//========================================

This function "f1" is executed inside the FOR LOOP (with 100 iterations). Totally, it takes a very long time (>30 minutes) to run the code. I was thinking about the vectorization of the code, but it doesn't work for the structure "s".

I would appreciate any help. Perhaps, someone could share the experience. Thanks a lot.

Subject: Problem with the time-efficiency of the code

From: Matt J

Date: 10 Sep, 2011 08:19:13

Message: 2 of 3

"Liana" wrote in message <j4dvlg$beb$1@newscl01ah.mathworks.com>...
>
> P1 = []; P2 = []; P3 = [];
> for i = 1 : tessqty
> tess = s(i).tess;
> faces = [tess(:,[1 2 3]); tess(:,[2 3 4]); ...
> tess(:,[1 3 4]); tess(:,[1 2 4])];
> P1 = [P1; s(i).tess.X(faces(:,1),1) s(i).tess.X(faces(:,1),2) s(i).tess.X(faces(:,1),3)];
==================

Tsk,Tsk. Growing the arrays P1,P2,P3 inside a for-loop? You should know better....






> This function "f1" is executed inside the FOR LOOP (with 100 iterations). Totally, it takes a very long time (>30 minutes) to run the code. I was thinking about the vectorization of the code, but it doesn't work for the structure "s".
=====================

So, about 20 sec per iteration of f1?

Anyway, apart from my point above, we have no way of knowing how many iterations your for-loop runs for, or how much overhead is contributed by some of the functions you're calling like rayTriangleIntersection. See "doc profile" for ways of analyzing this.

Subject: Problem with the time-efficiency of the code

From: Matt J

Date: 10 Sep, 2011 08:23:10

Message: 3 of 3

"Matt J" wrote in message <j4f6i1$i9g$1@newscl01ah.mathworks.com>...
>
>
> Tsk,Tsk. Growing the arrays P1,P2,P3 inside a for-loop? You should know better....
==================

In particular, recall this thread, where we talked about not doing this

http://www.mathworks.com/matlabcentral/newsreader/view_thread/306613#832590

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us