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;
- std::array<int, arrLength> arr;
- for ( size_t i = 0; i < arrLength; i++ ) {
- int value = rand () % arrLength;
- arr[i] = value;
- }
- // Start timer
- auto start = high_resolution_clock::now ();
- // Algo
- for ( int x = 0; x < 4000; ++x ) {
- for ( int y = 0; y < 4000; ++y ) {
- sum += arr[x * 4000 + y];
- }
- }
- // Stop timer and count duration
- auto stop = high_resolution_clock::now ();
- auto duration = duration_cast<microseconds> (stop - start);
- cout << "Finished after" << duration.count () << endl;
- // Start timer
- start = high_resolution_clock::now ();
- // Algo
- for ( int y = 0; y < 4000; ++y ) {
- for ( int x = 0; x < 4000; ++x ) {
- sum += arr[x + 4000 * y];
- }
- }
- // Stop timer and count duration
- stop = high_resolution_clock::now ();
- duration = duration_cast<microseconds> (stop - start);
- cout << "Finished after" << duration.count () << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment