c 2b 2b make vector loop more faster

Solutions on MaxInterview for c 2b 2b make vector loop more faster by the best coders in the world

showing results for - "c 2b 2b make vector loop more faster"
Vincent
15 Jun 2016
1#include <vector>
2#include <iostream>
3#include <ctime>
4using namespace std;
5
6int main() {
7    const int BIG = 20000000;
8    vector <int> v;
9    for ( int i = 0; i < BIG; i++ ) {
10        v.push_back( i );
11    }
12
13    int now = time(0);
14    cout << "start" << endl;
15    int n = 0;
16    for(vector<int>::iterator it = v.begin(); it != v.end(); ++it) {
17        n += *it;
18    }
19
20    cout << time(0) - now << endl;
21    now = time(0);
22    for(size_t i = 0; i < v.size(); ++i) {
23        n += v[i];
24    }
25    cout << time(0) - now << endl;
26
27    return n != 0;
28}
similar questions
queries leading to this page
c 2b 2b make vector loop more faster