Jump to content
  • Advertisement
Sign in to follow this  
thedodgeruk

iterators are they as fast as pointers?

This topic is 2601 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

i have a manager class, with funtions , and it itereates through the entitys in my program

all calculations are done through the iterator , and funtion calls.

eg (*m_baseList_it)->m_time = 0;


would it be best to do

CBaseEntity* temp = (*m_baseList_it);
temp->m_time = 0;


for speed i mean

Share this post


Link to post
Share on other sites
Advertisement
The only way to be sure would be to profile. I have a hunch (though it's just a hunch) that iterators are just as efficient as pointers...although I have no experience to back that up.

What I do have experience with is looking at the resulting assembly that get's spat out by the compiler...and seeing if it's doing a good job...which generally it is.

Share this post


Link to post
Share on other sites
In both ways, you are using iterators and deferencing it to get your object pointer. The deference operation (*m_baseList_it) will probably takes some compute time (nanoseconds), so it's faster to do it only once if you use the object pointer multiple times. In you example, since you only change one variable, it's exactly the same.

I though your question would be about speed difference between list/iterator and array/pointers iteration.

Share this post


Link to post
Share on other sites
Either should be fine. If you are doing multiple operations I like to hoist it to a reference for clarity:

for(container<smart_ptr<Foo>>::iterator i = foos.begin() ; i != foos.end() ; ++i)
{
Foo &foo = **i;

if(foo.isBar())
{
foo.frobnicate();
}
}

Share this post


Link to post
Share on other sites
Iterators should compile down to the exact same assembly code as pointers.

Unfortunately, a certain popular compiler enables iterator debugging for you by default, which makes them slow as hell unless you turn that feature off.

Share this post


Link to post
Share on other sites
I suspect that the compiler is probably doing the Right Thing and producing the same output for both. Why not check the disassembly and find out?

Share this post


Link to post
Share on other sites

I suspect that the compiler is probably doing the Right Thing and producing the same output for both. Why not check the disassembly and find out?


Iterators are probably safer as whoever wrote the code for them was probably better than you trying to duplicate the functionality of an iterator to use pointers instead.

The more significant performance impact would be whether your data you are iterating through is contiguous.

Share this post


Link to post
Share on other sites

[quote name='mhagain' timestamp='1306888367' post='4818097']
I suspect that the compiler is probably doing the Right Thing and producing the same output for both. Why not check the disassembly and find out?


Iterators are probably safer as whoever wrote the code for them was probably better than you trying to duplicate the functionality of an iterator to use pointers instead.[/quote]

Good point that. Plus the code behind the scenes of the iterator will be more robust, used in the wild for longer, tested, bugfixed, working, with all the weird little edge-cases where things go wrong worked through.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!