iterators are they as fast as pointers?

Started by
11 comments, last by Tachikoma 12 years, 10 months ago
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
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.
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.
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();
}
}
'Frobnicate' is now my favourite word.
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.
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?

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.


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.

[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.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.


'Frobnicate' is now my favourite word.


Hey!

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

This topic is closed to new replies.

Advertisement