Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7648361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:41:48+00:00 2026-05-31T10:41:48+00:00

When using std::vector is it always faster when going trough all vector’s elements via

  • 0

When using std::vector is it always faster when going trough all vector’s elements via indexes than using of iterators?

I wrote simple stupid test, VS 2010, optimization is disabled

#include <vector>
#include <iostream>
#include <ctime>

const int SIZE = 100000;    

int main()
{    
    std::vector<int> vInt;
    int i, temp;
    srand(time(0));
    for(i = 0; i<SIZE; i++)
        vInt.push_back(rand());

    time_t startTime, endTime;
    std::vector<int>::iterator it = vInt.begin(), itEnd = vInt.end();
startTime = clock();
for( ; it != itEnd; it++)
        temp = *it;
    endTime = clock();
    std::cout<<"Result for iterator: "<<endTime - startTime;

    i = 0;
    int size = vInt.size();
    startTime = clock();
    for(; i<size; i++)
        temp = vInt[i];
    endTime = clock();
    std::cout<<"\nResult for index: "<<endTime - startTime;    

    return 0;
}

DEBUG mode:

Without optimization results are

Result for iterator: 143
Result for index: 5

for const int SIZE = 1000;

Result for iterator: 0
Result for index: 0

for const int SIZE = 10000;

Result for iterator: 15
Result for index: 2

for const int SIZE = 1000000;

Result for iterator: 1377
Result for index: 53

With /O2 flag

for const int SIZE = 10000;

Result for iterator: 0 - 2
Result for index: 0

for const int SIZE = 100000;

Result for iterator: 12 - 15
Result for index: 0

for const int SIZE = 1000000;

Result for iterator: 133 - 142
Result for index: 0 - 3

So it is better always to use indexes with vector?

UPDATE:

RELEASE MODE

With /O2 flag all results are 0.

When optimization is disabled /Od indexes are faster

for const int SIZE = 100000000;

Result for iterator: 2182
Result for index: 472

for const int SIZE = 1000000;

Result for iterator: 22
Result for index: 5

for const int SIZE = 100000;

Result for iterator: 2 - 3
Result for index: 0 - 1
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T10:41:49+00:00Added an answer on May 31, 2026 at 10:41 am

    The first thing is that you should use whatever is idiomatic for your use case. If you are iterating I would use iterators, if you are performing random access, then indices.

    Now to the actual question. Performance is hard, even measuring performance is a hard problem and it requires that you have a clear idea of what you want to measure, how you are going to measure and how different things will affect your test. Ideally you want to isolate the test so that the measurement is as precise as possible, and then run the test multiple times to verify that the results are consistent. You should never even think on measuring performance with other than fully optimized code, and ideally with a real program. If you are in debug mode and the program is slow, the best optimization is just compiling in release mode, incrementing the optimization levels, reducing debug constructs from the libraries. All of those improvements come for free.

    In your test there are too many unknowns and variables to actually make anything out of it. The compiler flags and options can have a great impact so learn how to make your compiler produce the faster code. A simple implementation of an iterator for a vector is a plain pointer, so you can use this to get a base measurement:

    int *it=&v[0], *end=it+v.size();
    for (; it!=end; ++it) temp=*it;
    

    This should give you a base line to which compare your iterator. Any difference in performance between the iterator and this base line is due to extra checks that you compiler/library vendor is using for debugging. Read the docs on how to disable them. Also note that your step (it++) requires creating one copy of it, in the pointer case that basically has no effect, but if the iterator maintains any state at all, the cost of it++ will dominate the whole loop. Always prefer ++it.

    The next thing is what You want to measure and what the compiler thinks you need. You want to measure iteration, but the compiler does not know it, it only sees your code, and the optimizer will do its best to produce equivalent code that is as fast as possible. Taking just one of the loops, the compiler can realize that the whole iteration has no side effects other than setting temp to v[v.size()-1] and in the worst case (for your measurement) it can actually perform that transformation, removing the loops completely, which leads us to the next point:

    The devil is in the details. My guess is that your intention is measuring the relative costs of iteration in the general case, but your program is measuring the cost of iterating over a vector of constant size. Why does this matter? Compilers perform loop unrolling to avoid the cost of the test whenever they can. If the compiler knows that your loop will always contain a multiple of X iterations, then it can test for loop completion in only one of each X steps. Optimizations are not guaranteed, and in the general case of an application, the compiler will not know the number of iterations, so you should ensure that the test does not provide the compiler with more chances to optimize than it will have in your real program. In these two cases, you want to make sure that the compiler does not have extra information that it would not have in the real case, that is, you want to hide knowledge of the test and force it to focus on the problem. I would suggest that you move the loops into functions in a different translation unit, that you pass the vector by reference, and that you ensure that it cannot avoid looping (take an integer as argument and apply a binary operator with the temporary and each element in the vector, return the result of all the operations to the caller; while processing that function, the compiler will hopefully not be able to do anything too smart)

    But above all, I must refer you back to the first paragraph, do what’s idiomatic. Compilers are optimized for idiomatic code, and when/if performance is needed, they will do the right thing. The performance of the loop in this answer, or the two loops in the question is the same with unchecked iterators in an optimized build, not that the cost of the iteration itself will usually have any impact at all in the performance of your application.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using the 'using' declaration in C++ to add std::string and std::vector to the
When using the std::for_each, class A; vector<A*> VectorOfAPointers; std::for_each(VectorOfAPointers.begin(), VectorOfAPointers.end(), std::mem_fun(&A::foo)); If we have
#include <iostream> using namespace std; // This first class contains a vector and a
#include <iostream> #include <vector> using namespace std; int main() { vector< vector<int> > dp(50000,
#include <iostream> #include <vector> using namespace std; int main(void) { int i, s, g;
Consider the following code: #include <iostream> #include <memory> #include <vector> using namespace std; struct
#include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; int main(void) {
Instead of using std::vector<Object> ObjectArray; I would like it to be MyArray<Object> ObjectArray; with
#include <vector> using std::vector; class A { public: A() { buf.push_back(65); buf.push_back(66); buf.push_back(67); }
Please consider the following example: #include <string> #include <vector> using std::string; using std::vector; template

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.