Sign in to follow this  
thedodgeruk

iterators are they as fast as pointers?

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
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:
[code]
for(container<smart_ptr<Foo>>::iterator i = foos.begin() ; i != foos.end() ; ++i)
{
Foo &foo = **i;

if(foo.isBar())
{
foo.frobnicate();
}
}
[/code]

Share this post


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

Unfortunately, a certain popular compiler enables [url="http://msdn.microsoft.com/en-us/library/aa985896(v=vs.80).aspx"]iterator debugging[/url] 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
[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?
[/quote]

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='way2lazy2care' timestamp='1306898749' post='4818142']
[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?
[/quote]

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
[quote name='BattleMetalChris' timestamp='1306880989' post='4818065']
'[url="http://dictionary.reference.com/browse/frobnicate"]Frobnicate[/url]' is now my favourite word.
[/quote]

Hey!

I generally go by "frobnicator", but since many places used to limit name lengths I became just "frob" on this board and others. :)

Share this post


Link to post
Share on other sites
[font=arial, verdana, tahoma, sans-serif][size=2][quote name='GregMichael' timestamp='1306879260' post='4818053']
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.
[/quote]

[/size][/font]
Regular pointers are iterators. This is by design. For many implementations, the iterator for some classes such as std::vector are simply raw pointers.


Several iterator classes do additional work, but that is the design of the object. Going to the next object of a linked list, binary tree, or hashed container obviously requires some processing. Generally the processing is minimal.

Lots of expert programmers have reviewed the code; for a general purpose library the major implementations are extremely good. Obviously there are occasional improvements you can make by removing the general purpose nature of the library, but then it would no longer be the standard library.
[quote name='Hodgman' timestamp='1306888291' post='4818095']
Unfortunately, a certain popular compiler enables [url="http://msdn.microsoft.com/en-us/library/aa985896(v=vs.80).aspx"]iterator debugging[/url] for you by default, which makes them slow as hell unless you turn that feature off.
[/quote]

Only a few items have iterator debugging in the Visual C++ implementation, and they are easily seen in the documentation. Most of them do not. And debugging features are generally best turned off in final builds, iterator debugging included.

Share this post


Link to post
Share on other sites
I come from a C background so have been using pointers for many years...just getting to grips with C++ in the last few years....still got a way to go :)

I've just got to get "frobnicate" in the next game I do...even if it's in the special thanks section for the credits - LOL !

Share this post


Link to post
Share on other sites
I wouldn't worry about the performance issues too much, aim for a more robust coding practices. One situation where iterator overheads should be investigated is when operating on very large, tightly-packed POD data sets, and buffers, etc.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this