|
-
January 7th, 2011, 08:31 AM
#1
fastest vector iteration
hallo alle,
i need to know whats the fastest way to iterate through vector
this:
Code:
for( vector<MyObj*>::iterator it = vMyVector.begin(); it != vMyVector.end(); it++ )
{
MyObj * ptr = *it;
//do something
}
or this:
Code:
for( int i = 0; i < vMyVector.size(); i++ )
{
MyObj * ptr = vMyVector[i];
//do something
}
greetings,
Last edited by ProgrammerC++; January 7th, 2011 at 08:48 AM.
-
January 7th, 2011, 08:53 AM
#2
Re: fastest vector iteration
Why didn't you try to measure these both variants and to compare? 
Note that you should measure only Release builds
Victor Nijegorodov
-
January 7th, 2011, 08:54 AM
#3
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.
-
January 7th, 2011, 09:02 AM
#4
Re: fastest vector iteration
Ok I'll just build my own array wrapper class it's the fastest of all
-
January 7th, 2011, 09:07 AM
#5
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.
-
January 7th, 2011, 09:18 AM
#6
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.
-
January 7th, 2011, 09:50 AM
#7
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.
-
January 7th, 2011, 04:41 PM
#8
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.
Last edited by Chris_F; January 7th, 2011 at 04:57 PM.
-
January 7th, 2011, 05:03 PM
#9
Re: fastest vector iteration
 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
}
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
January 7th, 2011, 05:14 PM
#10
Re: fastest vector iteration
 Originally Posted by D_Drmmr
Code:
end = vMyVector.end()
Is this optimization really necessary? What compiler isn't going to automatically optimize this?
-
January 7th, 2011, 05:35 PM
#11
Re: fastest vector iteration
That depends on how certain the compiler is that end() isn't going to change during the iteration.
-
January 8th, 2011, 05:21 AM
#12
Re: fastest vector iteration
 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.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|