Note that there are some explanatory texts on larger screens.

plurals
  1. POimproving the speed of the execution of that piece of code
    primarykey
    data
    text
    <p>I have two methods used to render a grid. First one :</p> <pre><code>void Grid::openglRender(){ glPolygonMode( GL_FRONT_AND_BACK, GL_LINE ); glBegin(GL_TRIANGLES); glColor3f(1.0f, 1.0f, 0.0f); Node* A, * B, * C, * D; for(size_t X=0 ; X&lt;sizeX-1 ; X++)for(size_t Z=0 ; Z&lt;sizeZ-1; Z++){ A = &amp;nodes[X*sizeZ+Z]; B = &amp;nodes[(X+1)*sizeZ+Z]; C = &amp;nodes[X*sizeZ+(Z+1)]; D = &amp;nodes[(X+1)*sizeZ+(Z+1)]; glVertex3f(A-&gt;x, A-&gt;y, A-&gt;z); glVertex3f(B-&gt;x, B-&gt;y, B-&gt;z); glVertex3f(C-&gt;x, C-&gt;y, C-&gt;z); glVertex3f(B-&gt;x, B-&gt;y, B-&gt;z); glVertex3f(D-&gt;x, D-&gt;y, D-&gt;z); glVertex3f(C-&gt;x, C-&gt;y, C-&gt;z); } glEnd(); }; </code></pre> <p>Second one :</p> <pre><code>void Grid::openglRender(){ glPolygonMode( GL_FRONT_AND_BACK, GL_LINE ); glBegin(GL_TRIANGLES); glColor3f(1.0f, 1.0f, 0.0f); for(size_t X=0 ; X&lt;sizeX-1 ; X++)for(size_t Z=0 ; Z&lt;sizeZ-1; Z++){ glVertex3f(nodes[X*sizeZ+Z].x, nodes[X*sizeZ+Z].y, nodes[X*sizeZ+Z].z); glVertex3f(nodes[(X+1)*sizeZ+Z].x, nodes[(X+1)*sizeZ+Z].y, nodes[(X+1)*sizeZ+Z].z); glVertex3f(nodes[X*sizeZ+(Z+1)].x, nodes[X*sizeZ+(Z+1)].y, nodes[X*sizeZ+(Z+1)].z); glVertex3f(nodes[(X+1)*sizeZ+Z].x, nodes[(X+1)*sizeZ+Z].y, nodes[(X+1)*sizeZ+Z].z); glVertex3f(nodes[(X+1)*sizeZ+(Z+1)].x, nodes[(X+1)*sizeZ+(Z+1)].y, nodes[(X+1)*sizeZ+(Z+1)].z); glVertex3f(nodes[X*sizeZ+(Z+1)].x, nodes[X*sizeZ+(Z+1)].y, nodes[X*sizeZ+(Z+1)].z); } glEnd(); }; </code></pre> <p>for me the first one looks better in term of number of operations, in <code>glVertex3f</code> I just use the pointer to get a value. In the second method each time I have to multiply and to add something.</p> <p>But at the running time I don't realy feel a difference. So I'm right when I say that the first one is better ? or maybe whatever I chose the compiler know better than I, what to do to get the best...</p> <p>maybe it would be a bit better if I declare <code>X</code> and <code>Z</code> before the <code>for</code> loops to avoid especially the declaration and destruction <code>sizeX</code> times of Z</p> <p>Also I guess, the best would be to create a list (one time, stored ro be reused every frame) with all nodes in the order to be traversed to create the grid instead of using two <code>for</code></p>
    singulars
    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.
 

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