Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <array>
- #include <chrono>
- #include <iostream>
- #include <numeric>
- #include <random>
- #include <vector>
- using namespace std;
- using chrono::system_clock;
- using chrono::high_resolution_clock;
- using chrono::duration_cast;
- using chrono::microseconds;
- int main() {
- vector<int> v;
- unsigned seed = system_clock::now().time_since_epoch().count();
- mt19937_64 gen(seed);
- uniform_int_distribution<int> dist(0, 9);
- for (int i = 0; i < 300000; i++) v.push_back(dist(gen));
- auto t0 = high_resolution_clock::now();
- long acc = accumulate(v.begin(), v.end(), 0); cout << "acc(vec): " << acc << ", ";
- auto t1 = high_resolution_clock::now();
- cout << duration_cast<microseconds>(t1 - t0).count() << " ms" << endl;
- t0 = high_resolution_clock::now();
- acc = 0; for (auto& e: v) acc += e; cout << "for(vec): " << acc << ", ";
- t1 = high_resolution_clock::now();
- cout << duration_cast<microseconds>(t1 - t0).count() << " ms" << endl;
- array<int, 300000> a;
- for (auto& e: a) e = dist(gen);
- t0 = high_resolution_clock::now();
- acc = accumulate(a.begin(), a.end(), 0); cout << "acc(arr): " << acc << ", ";
- t1 = high_resolution_clock::now();
- cout << duration_cast<microseconds>(t1 - t0).count() << " ms" << endl;
- t0 = high_resolution_clock::now();
- acc = 0; for (auto& e: a) acc += e; cout << "for(arr): " << acc << ", ";
- t1 = high_resolution_clock::now();
- cout << duration_cast<microseconds>(t1 - t0).count() << " ms" << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement