Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The following program measures the time required to access an array 500000 times

ID: 3830821 • Letter: T

Question

The following program measures the time required to access an array 500000 times using array indexing. Rewrite the program so that your program accesses the array using pointer notation. Execute both programs multiple times and compare execution times. Assess the results with your own words in less than 100 words.
#include<iostream> #include<chrono> using namespace std; using namespace std::chrono; void main() {      int i, loop, ary[1000] = {};      double sum = 0.0;      __int64 duration;      high_resolution_clock::time_point t1, t2;      t1 = high_resolution_clock::now();      for (i = 0; i < 1000; i++)           ary[i] = 1;      for (loop = 0; loop < 500000; loop++)      {           for (i = 0; i < 1000; i++)               sum += ary[i];      }      cout << "sum = " << sum << endl;      t2 = high_resolution_clock::now();      duration = std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count();      cout << duration << endl << endl; } // main. The following program measures the time required to access an array 500000 times using array indexing. Rewrite the program so that your program accesses the array using pointer notation. Execute both programs multiple times and compare execution times. Assess the results with your own words in less than 100 words.
#include<iostream> #include<chrono> using namespace std; using namespace std::chrono; void main() {      int i, loop, ary[1000] = {};      double sum = 0.0;      __int64 duration;      high_resolution_clock::time_point t1, t2;      t1 = high_resolution_clock::now();      for (i = 0; i < 1000; i++)           ary[i] = 1;      for (loop = 0; loop < 500000; loop++)      {           for (i = 0; i < 1000; i++)               sum += ary[i];      }      cout << "sum = " << sum << endl;      t2 = high_resolution_clock::now();      duration = std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count();      cout << duration << endl << endl; } // main. The following program measures the time required to access an array 500000 times using array indexing. Rewrite the program so that your program accesses the array using pointer notation. Execute both programs multiple times and compare execution times. Assess the results with your own words in less than 100 words.
#include<iostream> #include<chrono> using namespace std; using namespace std::chrono; void main() {      int i, loop, ary[1000] = {};      double sum = 0.0;      __int64 duration;      high_resolution_clock::time_point t1, t2;      t1 = high_resolution_clock::now();      for (i = 0; i < 1000; i++)           ary[i] = 1;      for (loop = 0; loop < 500000; loop++)      {           for (i = 0; i < 1000; i++)               sum += ary[i];      }      cout << "sum = " << sum << endl;      t2 = high_resolution_clock::now();      duration = std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count();      cout << duration << endl << endl; } // main.

Explanation / Answer

below is your program: -

#include<iostream>
#include<chrono>
using namespace std;
using namespace std::chrono;

int main()
{
int i, loop, ary[1000] = {};
double sum = 0.0;
__int64 duration;
high_resolution_clock::time_point t1, t2,t3,t4;

t1 = high_resolution_clock::now();

for (i = 0; i < 1000; i++)
ary[i] = 1;

for (loop = 0; loop < 500000; loop++)
{
for (i = 0; i < 1000; i++)
sum += ary[i]; // direct array access
}

cout << "withoud pointer sum = " << sum << endl;

t2 = high_resolution_clock::now();

duration = std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count();

cout << "withoud pointer duration:"<<duration << endl << endl;
sum= 0.0;
   t3 = high_resolution_clock::now();

for (i = 0; i < 1000; i++)
*(ary+i) = 1;

for (loop = 0; loop < 500000; loop++)
{
for (i = 0; i < 1000; i++)
sum += *(ary+i); // array access using pointers
}

cout << "with pointer sum = " << sum << endl;

t4 = high_resolution_clock::now();

duration = std::chrono::duration_cast<std::chrono::microseconds>(t4 - t3).count();

cout << "with pointer duration:"<<duration << endl << endl;
  
return 0;
} // main.

Sample Run: -

withoud pointer sum = 5e+008
withoud pointer duration:1952503

with pointer sum = 5e+008
with pointer duration:1937403


--------------------------------
Process exited after 4.037 seconds with return value 0
Press any key to continue . . .

2.

withoud pointer sum = 5e+008
withoud pointer duration:2045117

with pointer sum = 5e+008
with pointer duration:2015115


--------------------------------
Process exited after 4.194 seconds with return value 0
Press any key to continue . . .

3.

withoud pointer sum = 5e+008
withoud pointer duration:2018115

with pointer sum = 5e+008
with pointer duration:2013115


--------------------------------
Process exited after 4.162 seconds with return value 0
Press any key to continue . . .

4.

withoud pointer sum = 5e+008
withoud pointer duration:2003812

with pointer sum = 5e+008
with pointer duration:1988009


--------------------------------
Process exited after 4.11 seconds with return value 0
Press any key to continue . . .

5.

withoud pointer sum = 5e+008
withoud pointer duration:2000808

with pointer sum = 5e+008
with pointer duration:1968810


--------------------------------
Process exited after 4.095 seconds with return value 0
Press any key to continue . . .

So the difference in time taken for accessing the array normally and with pointer is as below:-

So, accessing array using pointer is much faster than accessing array normally. It is because when we try to access array normally, only the first address i.e. address of first element is known, and we have to deduce next addresses accordingly.

But when we access arrays using pointers, all the addresses are known and no new address computation is required.

Because of less computation required for each array retrieval, time taken to access each element is less. So there is time difference as shown above.

without pointer with pointer 1952503 1937403 15100 2045117 2015115 30002 2018115 2013115 5000 2003812 1988009 15803 2000808 1968810 31998 Average 19580.6