Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>No matter how hard I study the code that's been posted, I can't see why the call to <code>vdist</code> is made inside the loop. </p> <p>When I'm trying to optimise a block of code inside a loop one of the things I look for are statements which are invariant, that is which are the same at each call, and which can therefore be lifted out of the loop.</p> <p>Looking at</p> <pre><code>130 for res=1:6 131 U_neu=p0+NV1; 132 [pos,a12] = vdist(p0(:,2),p0(:,1),U_neu(:,2),U_neu(:,1)); 133 a12=a12+1/6.*res*totalRotation; 134 ddist=1852*safety_distance; 135 [lat2,lon2] = vreckon(p0(:,2),p0(:,1),ddist, a12); 136 extendedPoly(f,:)=[lon2,lat2];f=f+1; 137 end </code></pre> <p>I see</p> <ul> <li>in l131 the variables <code>p0</code>, <code>NV1</code> appear only on the rhs, and they only appear on the rhs elsewhere inside the loop, so this statement is loop-invariant and can be lifted out of the loop; only a small time saving perhaps;</li> <li>in l134 again, I see another loop-invariant statement, which can again be lifted out of the loop for another small time saving;</li> <li>but then I started to look very closely, and I can't see why l132, where the call to <code>vdist</code> is made, is inside the loop either. None of the values on the rhs of that assignment are modified in the loop (other than <code>U_neu</code> but I've already lifted that out of the loop).</li> </ul> <p>Tidying up what was left a bit, this is what I ended up with:</p> <pre><code>U_neu=p0+NV1; [pos,a12] = vdist(p0(:,2),p0(:,1),U_neu(:,2),U_neu(:,1)); ddist=1852*safety_distance; for res=1:6 extendedPoly(f,:) = vreckon(p0(:,2),p0(:,1),ddist, a12+1/6.*res*totalRotation); f=f+1; end </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload