|
From: | Daniel Arteaga |
Subject: | Re: Any possible optimization? |
Date: | Tue, 11 Jan 2011 10:28:58 +0100 |
User-agent: | Mozilla/5.0 (X11; U; Linux i686; ca-ES; rv:1.9.2.13) Gecko/20101207 Thunderbird/3.1.7 |
Al 10/01/11 18:39, En/na CdeMills ha escrit:
If the function is called many times from the same place, the best way is to not use function call at all and vectorise the code. Let say that your points coordinates are in row matrices: aa = b-a; bb = c-d; cc = c-a; my_idet = 1./(aa(:, 1).*bb(:, 2)-aa(:, 2).*bb(:, 1)); s = (bb(:, 2).*cc(:, 1) - bb(:, 1).*cc(:, 1)).*my_idet; t = (aa(:, 1).*cc(:, 2) - aa(:, 2).*cc(:, 1)).*my_idet; This way, there is no 'for' loop, nor function call.
I cannot vectorize this part of my code since the calculation at a given iteration depends on the previous iteration. Any suggestion of how this situation can be handled more efficiently is welcome.
To give background, I'm exploring a terrain map composed of triangles, and the goal is to obtain a section of the map by intersecting a given direction with the vertices of the triangles. The triangle to explore depends on the previous segment intersected.
The names of variables should not alias function names (det). The last small optimisation is that (I learned it a long time ago), floating point division is slower than multiplication, so the inverse of the determinant is computed only once.
I just implemented this, and the performance gains are very modest. Interesting to take into account nevertheless.
Thank you very much, Daniel
[Prev in Thread] | Current Thread | [Next in Thread] |