Jump to content
  • Advertisement
Sign in to follow this  
hotdogsayhi

which following way of using std::vector is faster?

This topic is 3543 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

using namespace std;

vector<int> myIntVector;
myIntVector.push_back(1);
myIntVector.push_back(4);
myIntVector.push_back(8);

for(int y=0; y<myIntVector.size(); y++)
{
    cout<<myIntVector[y]<<" ";
}

using namespace std;

vector<int> myIntVector;
vector<int>::iterator myIntVectorIterator;

myIntVector.push_back(1);
myIntVector.push_back(4);
myIntVector.push_back(8);

for(myIntVectorIterator = myIntVector.begin(); 
        myIntVectorIterator != myIntVector.end();
        myIntVectorIterator++)
{
    cout<<*myIntVectorIterator<<" ";
}


If there is not difference in speed, what is the difference? thx

Share this post


Link to post
Share on other sites
Advertisement
The difference is that you wouldn't write the second option, you would write instead:
std::copy(myIntVector.begin(), myIntVector.end(),
std::ostream_iterator<int>(std::cout, " "));

Share this post


Link to post
Share on other sites
Most of your time will probably be spent in std::cout :P I assume you're asking what the difference in speed is between operator [] and iterators? In most implementations of the STL the difference will be pretty much none. However in the Microsoft version of the STL written by Dinkumware iterators are checked for validity as is the index of operator []. The checked iterators can be disabled by defining _SECURE_SCL=0 and _HAS_ITERATOR_DEBUGGING=0 if I remember correctly. The index check on operator [] is impossible to disable as far as I know. This applies to MSVC++ 9 and possibly 8. Most likely you really don't care about this to be honest.

Share this post


Link to post
Share on other sites
Quote:
Original post by asp_
Most of your time will probably be spent in std::cout :P I assume you're asking what the difference in speed is between operator [] and iterators? In most implementations of the STL the difference will be pretty much none. However in the Microsoft version of the STL written by Dinkumware iterators are checked for validity as is the index of operator []. The checked iterators can be disabled by defining _SECURE_SCL=0 and _HAS_ITERATOR_DEBUGGING=0 if I remember correctly. The index check on operator [] is impossible to disable as far as I know. This applies to MSVC++ 9 and possibly 8. Most likely you really don't care about this to be honest.

does it mean iterators with index check disabled will faster than [] operator?

Share this post


Link to post
Share on other sites
The biggest difference I know of is for the second, you write horrifically long variable names. What I'd write:
  for( std::vector::iterator it=ints.begin(); it != ints.end(); it++ )
std::cout << *it << ' ';
However, ToohrVyk gave an much better solution.

asp_: You can disable those checks by running in release mode.

Is *it faster than or slower than v?
Theoretically, no. For the fallowing:
  int* ints = new int[5];
for( int* i=ints; i != ints+5; i++ )
cout << *i << ' ';
or...
  std::copy( ints, ints+5, std::ostream_iterator<int>(cout, " ") );
the assembly pseudo code is something like:
init:
i = ints
terminate = ints+5 // A likely compiler optimization.
cond:
if i == terminate goto end
body:
char c = *i
cout << c << ' '
goto cond
end:
Now, for v:
init:
i = ints
terminate = ints+5 // A likely compiler optimization.
cond:
if i == terminate goto end
body:
int* address = ints + i * 4 // Can be done in one clock cycle.
char c = *i
cout << c << ' '
goto cond
end:


The difference between these two is minimal, and your compiler may be able to do the iterator one, even if you use the index one. Bjarne Stroustrup's advice? Go with whichever you like more.

Share this post


Link to post
Share on other sites
Quote:
Original post by Splinter of Chaos
asp_: You can disable those checks by running in release mode.


according to msdn:
Checked iterators apply to release builds and debug builds.


Oh and feel free to use postincrement the compiler optimizes it easily.

Share this post


Link to post
Share on other sites
Splinter,
No you can't. It should be disabled with _SECURE_SCL which it apparently is now. Not sure when that changed.. Used to be impossible to disable it but looking through the yvals.h header now it seems like the _SCL_SECURE_VALIDATE_RANGE is correctly axed with _SECURE_SCL=0. However if you disable _SECURE_SCL you can't use any libraries which are built with _SECURE_SCL enabled, including the boost binaries built and distributed by boost consulting.

hotdogsayhi,
Anyways in most situations operator [] and iterators will be equal in performance. However iterators have many advantages such as what ToohrVyk showed you.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!