Note that there are some explanatory texts on larger screens.

plurals
  1. POCan I optimize code that has 3 for loops and 4 ifs?
    text
    copied!<p>i made another post</p> <p><a href="https://stackoverflow.com/questions/326885/a-loop-to-create-neighbor-nodes-in-3d-space">here</a> where I asked how to create the 26 neighbors of a cubic-voxel-node in 3-d space. I got a very good answer and implemented it. </p> <p>To that I added some MIN MAX Position checking.</p> <p>I would like to know if there is way, <strong>in relationship to the 3 for loops and 4 if used</strong>, to improve the execution time of this code. I have read in another post sth that when using while loops is faster but it was in a post not language specific. </p> <p>Is this true? If yes, could u please help me to this in my code because i luck experience? Is there a way to implement this recursively in a way that will make it faster?</p> <p>here is my code:</p> <pre><code>... std::vector&lt;Pos&gt; Create26Neighbor(Pos somePos, double resol) { std::vector &lt;Pos&gt; vect1; Pos m_MinPos(0.0,0.0,0.0); Pos m_MaxPos(5.0,4.0,5.0); for (double dz = somePos.m_pPos[2] - resol; dz &lt;= somePos.m_pPos[2] + resol; dz+=resol) { if (dz&gt;m_MinPos.m_pPos[2] &amp;&amp; dz&lt;m_MaxPos.m_pPos[2]) { for (double dy = someCPos.m_pPos[1] - resol; dy &lt;= someCPos.m_pPos[1] + resol; dy+=resol) { if (dy&gt;m_MinPos.m_pPos[1] &amp;&amp; dy&lt;m_MaxPos.m_pPos[1]) { for (double dx = somePos.m_pPos[0] - resol; dx &lt;= somePos.m_pPos[0] + resol; dx+=resol) { if (dx&gt;m_MinPos.m_pPos[0] &amp;&amp; dx&lt;m_MaxPos.m_pPos[0]) { // all 27 if ((dx != somePos.m_pPos[0]) || (dy != somePos.m_pPos[1]) || (dz != somePos.m_pPos[2])) { Pos tempPos(dx,dy,dz); vect1.push_back(tempPos); } } } } } } } return vect1; } .... </code></pre>
 

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