CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Jul 2009
    Posts
    154

    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.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    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

  3. #3
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    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.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  4. #4
    Join Date
    Jul 2009
    Posts
    154

    Re: fastest vector iteration

    Ok I'll just build my own array wrapper class it's the fastest of all

  5. #5
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725

    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.

  6. #6
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    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.

  7. #7
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    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.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  8. #8
    Join Date
    Aug 2008
    Posts
    902

    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&#37; 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.

  9. #9
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: fastest vector iteration

    Quote Originally Posted by ProgrammerC++ View Post
    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

  10. #10
    Join Date
    Aug 2008
    Posts
    902

    Re: fastest vector iteration

    Quote Originally Posted by D_Drmmr View Post
    Code:
    end = vMyVector.end()
    Is this optimization really necessary? What compiler isn't going to automatically optimize this?

  11. #11
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: fastest vector iteration

    That depends on how certain the compiler is that end() isn't going to change during the iteration.

  12. #12
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: fastest vector iteration

    Quote Originally Posted by Chris_F View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured