Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <array>
- #include <chrono>
- #include <cstdlib>
- #include <ctime>
- #include <iostream>
- using namespace std::chrono;
- using namespace std;
- int main() {
- // Setup
- int sum = 0;
- const std::size_t arrLength = 4000 + 4000 * 4000;
- int *arr = new int[arrLength];
- for (size_t i = 0; i < arrLength; i++) {
- int value = rand() % arrLength;
- arr[i] = value;
- }
- // Algorithm #1
- auto start = high_resolution_clock::now();
- for (int x = 0; x < 4000; ++x) {
- for (int y = 0; y < 4000; ++y) {
- sum += arr[x * 4000 + y];
- }
- }
- auto stop = high_resolution_clock::now();
- auto duration = duration_cast<microseconds>(stop - start);
- cout << "Finished after " << duration.count() << endl;
- // Algorithm #2
- start = high_resolution_clock::now();
- for (int y = 0; y < 4000; ++y) {
- for (int x = 0; x < 4000; ++x) {
- sum += arr[x + 4000 * y];
- }
- }
- stop = high_resolution_clock::now();
- duration = duration_cast<microseconds>(stop - start);
- cout << "Finished after " << duration.count() << endl;
- return 0;
- }
Add Comment
Please, Sign In to add comment