exapsy

Untitled

Apr 3rd, 2021
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.23 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
  11.     main ()
  12. {
  13.     // Setup
  14.     int sum = 0;
  15.     const std::size_t arrLength = 4000 + 4000 * 4000;
  16.     std::array<int, arrLength> arr;
  17.  
  18.     for ( size_t i = 0; i < arrLength; i++ ) {
  19.     int value = rand () % arrLength;
  20.     arr[i] = value;
  21.     }
  22.  
  23.     // Start timer
  24.     auto start = high_resolution_clock::now ();
  25.  
  26.     // Algo
  27.     for ( int x = 0; x < 4000; ++x ) {
  28.     for ( int y = 0; y < 4000; ++y ) {
  29.         sum += arr[x * 4000 + y];
  30.     }
  31.     }
  32.  
  33.     // Stop timer and count duration
  34.     auto stop = high_resolution_clock::now ();
  35.     auto duration = duration_cast<microseconds> (stop - start);
  36.  
  37.     cout << "Finished after" << duration.count () << endl;
  38.  
  39.     // Start timer
  40.     start = high_resolution_clock::now ();
  41.  
  42.     // Algo
  43.     for ( int y = 0; y < 4000; ++y ) {
  44.     for ( int x = 0; x < 4000; ++x ) {
  45.         sum += arr[x + 4000 * y];
  46.     }
  47.     }
  48.  
  49.     // Stop timer and count duration
  50.     stop = high_resolution_clock::now ();
  51.     duration = duration_cast<microseconds> (stop - start);
  52.  
  53.     cout << "Finished after" << duration.count () << endl;
  54.  
  55.     return 0;
  56. }
  57.  
  58.  
Advertisement
Add Comment
Please, Sign In to add comment