Re: fastest vector iteration
Why didn't you try to measure these both variants and to compare? :confused:
Note that you should measure only Release builds
Re: fastest vector iteration
Why don't you write a simple timing loop to test it out?
You probably get much faster results instead of asking.
Besides, if you are concerned so much about speed, you need to look elsewhere. First check your algoritms and make alogirthmic speedups, those should give you the biggest boost.
Re: fastest vector iteration
Ok I'll just build my own array wrapper class it's the fastest of all :)
Re: fastest vector iteration
What makes you think that vector will be slower than a normal array ?
The only time it should be normal is when creating the vector ... after that there
should be no difference. (Internally, a vector uses a normal dynamically
allocated array).
From what I understand, VC++ has some checks on iterators that you should
disable if speed is a concern.
Also, looping over a container is fast (even for slow conterners like list). It
is what is done inside the loop that typically takes the time.
Re: fastest vector iteration
iterating over the loop with either method will be basically irrelevant.
the compiler will optimize and both methods will produce something similar. While one may be a tad faster than the other in some cases and other way around in some others, the difference will be negligeable.
You should worry far more about the performance of what you're doing in the "// Do something" (or why you even need a loop at all) rather than worry about the performance of the actual loop construct.
Re: fastest vector iteration
How many items are you storing in the container? Unless it's many millions or more, I don't think you should be worry about the speed difference between the two.
Re: fastest vector iteration
Containers shouldn't be iterated while checking against .size(), that's what .end() is for.
Make sure iterator debugging is off for release.
Code:
for( vector<MyObj*>::iterator it = vMyVector.begin(); it != vMyVector.end(); ++it )
{
//do something
}
I find that std::for_each is slightly faster than a for loop; about 6-10% faster in most of my tests.
Edit: Also, I would caution you on using a vector of pointers. Changing to a vector of shared_ptr may be advisable.
Re: fastest vector iteration
Quote:
Originally Posted by
ProgrammerC++
i need to know whats the fastest way to iterate through vector
Both are unnecessarily inefficient.
Code:
for( vector<MyObj*>::iterator it = vMyVector.begin(), end = vMyVector.end(); it != end; ++it)
{
MyObj * ptr = *it;
//do something
}
for(size_t i = 0, size = vMyVector.size(); i < size; ++i)
{
MyObj * ptr = vMyVector[i];
//do something
}
Re: fastest vector iteration
Quote:
Originally Posted by
D_Drmmr
Code:
end = vMyVector.end()
Is this optimization really necessary? What compiler isn't going to automatically optimize this?
Re: fastest vector iteration
That depends on how certain the compiler is that end() isn't going to change during the iteration.
Re: fastest vector iteration
Quote:
Originally Posted by
Chris_F
Is this optimization really necessary? What compiler isn't going to automatically optimize this?
Why don't you check for yourself? When it comes to performance, never assume and always measure.