exapsy

Untitled

Apr 3rd, 2021 (edited)
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.08 KB | None | 0 0
  1. #include <array>
  2. #include <chrono>
  3. #include <cstdlib>
  4. #include <ctime>
  5. #include <iostream>
  6.  
  7. using namespace std::chrono;
  8. using namespace std;
  9.  
  10. int main() {
  11.   // Setup
  12.   int sum = 0;
  13.   const std::size_t arrLength = 4000 + 4000 * 4000;
  14.   int *arr = new int[arrLength];
  15.  
  16.   for (size_t i = 0; i < arrLength; i++) {
  17.     int value = rand() % arrLength;
  18.     arr[i] = value;
  19.   }
  20.  
  21.   // Algorithm #1
  22.   auto start = high_resolution_clock::now();
  23.  
  24.   for (int x = 0; x < 4000; ++x) {
  25.     for (int y = 0; y < 4000; ++y) {
  26.       sum += arr[x * 4000 + y];
  27.     }
  28.   }
  29.  
  30.   auto stop = high_resolution_clock::now();
  31.   auto duration = duration_cast<microseconds>(stop - start);
  32.  
  33.   cout << "Finished after " << duration.count() << endl;
  34.  
  35.   // Algorithm #2
  36.   start = high_resolution_clock::now();
  37.  
  38.   for (int y = 0; y < 4000; ++y) {
  39.     for (int x = 0; x < 4000; ++x) {
  40.       sum += arr[x + 4000 * y];
  41.     }
  42.   }
  43.  
  44.   stop = high_resolution_clock::now();
  45.   duration = duration_cast<microseconds>(stop - start);
  46.  
  47.   cout << "Finished after " << duration.count() << endl;
  48.  
  49.   return 0;
  50. }
  51.  
Add Comment
Please, Sign In to add comment