Re: Performance: Iterating a vector in a loop..
- From: "andré m.a" <a.m.a@xxxxxxxxxxxx>
- Date: Thu, 26 May 2005 17:55:00 -0400
> But he was saying its slow because .begin() and .end() are called every
> loop.
>
> I know that the function is called every iteration, but was under the
> impression that STL is highly optimised in dealing with things like this,
> so it shouldnt make too much difference.
>
> Can anyone shed any light on this and explain whether or not it really has
> any speed hits when doing that, or, if it would be better to re-write the
> code like this (which i feel is less legible).
>
> vector<MyType>::iterator itLoop, itBegin = vec.begin(), itEnd= vec.end();
>
> for (itLoop = itBegin ; itLoop != itEnd; itLoop++)
> {
> itLoop->DoSomething();
> }
>
> Thanks for any info.
I dont think that begin() is required more then once.
But the fact that you only call end() once is logicaly an improvement.
and im not sure if
while( itLoop != itEnd )
{
(++itLoop)->DoSomething();
}
would be faster either. At this point if the vector is very large,
you may want to set up a test to verify these things yourself.
.
- Follow-Ups:
- Re: Performance: Iterating a vector in a loop..
- From: Simon Trew
- Re: Performance: Iterating a vector in a loop..
- References:
- Performance: Iterating a vector in a loop..
- From: Mark Ingram
- Performance: Iterating a vector in a loop..
- Prev by Date: Re: SHFileoperation question
- Next by Date: Tree Expand
- Previous by thread: Re: Performance: Iterating a vector in a loop..
- Next by thread: Re: Performance: Iterating a vector in a loop..
- Index(es):
Relevant Pages
|