• Advertisement

Archived

This topic is now archived and is closed to further replies.

STL performance question

This topic is 5809 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

not that i think this change will make a whole lot of difference in my performance but which is quicker of the following 2 choices: 1)
  
for(unsigned int i = 0; i < gameObjects.size(); i++) {
    glPushMatrix()
    glTranslatef(gameObjects[i]->pos.x, gameObjects[i]->pos.y, gameObjects[i]->pos.z);

    glCallList(gameObjects[i]->model->getDisplayList());

    glPopMatrix();
}
  
or 2)
  
for(unsigned int i = 0; i < gameObjects.size(); i++) {
    GameObject * object = gameObjects[i];

    glPushMatrix()
    glTranslatef(gameObject->pos.x, gameObject->pos.y, gameObject->pos.z);

    glCallList(gameObject->model->getDisplayList());

    glPopMatrix();
}
  
i.e. if i''m making repeated calls to vector is it faster to assign the pointer value to a dummy pointer on each iteration of the loop and make the calls from that dummy/copy. since it''s O(n) with the loop and all does a smallish optimization like this become more significant? -me

Share this post


Link to post
Share on other sites
Advertisement
its possibly faster to save the function call (assuming its not inlined) as well as the offset calculation (its also possible your STL implementation and compiler is smart enough to do this for you). look at the asm output and count the clock cycles if you really care, but it can't possible matter because there is no way you have anywhere near enough gameObjects to make this a noticable optimization. profile before you optimize, or you are wasting your time.

EDIT: also if you really cared, then number 2 should be like this:


      
GameObject * gameObject = &gameObjects[0];
usigned int size = gameObjects.size();
for(unsigned int i = 0; i < size; ++i, ++gameObject)
{
glPushMatrix();
glTranslatef(gameObject->pos.x, gameObject->pos.y,gameObject->pos.z);
glCallList(gameObject->model->getDisplayList());
glPopMatrix();
}



[edited by - invective on May 29, 2002 12:35:56 AM]

Share this post


Link to post
Share on other sites
Rather than calling operator[] each and every time, write an iterator (or pointer, for an array) based loop.


  
vector<GameObject>::iterator itor, end;
end = GameObjects.end(); // so that we don''t evaluate end() every time


for( itor = GameObjects.begin(); itor != end; ++itor )
{
glPushMatrix()
glTranslatef(itor->pos.x,
itor->pos.y,
itor->pos.z);
glCallList(itor->model->getDisplayList());
glPopMatrix();
}


Or write a function object with the code that would be in the loop inlined and called by operator() then just call for_each.


  
#include<algorithm>


struct RenderObject
{
inline void operator()( GameObject& obj )
{
glPushMatrix()
glTranslatef(obj.pos.x,
obj.pos.y,
obj.pos.z);
glCallList(obj.model->getDisplayList());
glPopMatrix();
}
};

...
std::for_each( GameObjects.begin(), GameObjects.end(), RenderObject() );


Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]

Share this post


Link to post
Share on other sites
If you can bear, look at the disassembly. In my experience, 99% of for (...) array[x] loops are converted into pointers (e.g. your second example) by the compiler. The two versions should compile to the same code.

Now, since this is STL, I note a number of things inefficient about these two loops:
- if gameObjects can''t change size in the middle of the loop, the call to .size () each time is potentially wasteful.
- instead of using indices, you could choose to be using iterators. I''m pretty sure it''s the same efficiency. But when you use iterators, use ++it instead of it++ in your for statement.
- these problems can''t happen if you use for_each.

Share this post


Link to post
Share on other sites

  • Advertisement