Last active
May 29, 2016 02:31
-
-
Save giuliano0/efba44b85da963527f8561efe4b5cce4 to your computer and use it in GitHub Desktop.
Testing indexed loops versus iterators (compile and run with -std=c++11 -O[1-3])
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <vector> | |
#include <iostream> | |
#include <chrono> | |
using namespace std; | |
int main() { | |
const size_t BIG = 200000000; | |
vector <int> v; | |
for (size_t i = 0; i < BIG; i++) { | |
v.push_back(i); | |
} | |
cout << "start" << endl; | |
int n = 0; | |
auto t_start = chrono::high_resolution_clock::now(); | |
for(vector<int>::iterator it = v.begin(); it != v.end(); ++it) { | |
n += *it; | |
} | |
auto t_end = chrono::high_resolution_clock::now(); | |
cout << chrono::duration<double, milli>(t_end - t_start).count() << endl; | |
t_start = chrono::high_resolution_clock::now(); | |
for(size_t i = 0; i < v.size(); ++i) { | |
n += v[i]; | |
} | |
t_end = chrono::high_resolution_clock::now(); | |
cout << chrono::duration<double, milli>(t_end - t_start).count() << endl; | |
return n != 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Code heavily based on a random stackoverflow topic about the subject.
I modified the time measuring so it uses chrono instead of clock.