Respuesta :
Answer:
#include <stdio.h>
#include <time.h>
int main()
{
Â
clock_t start, end;
double cpu_time_used;
Â
//starting the cpu clock.
start = clock();
Â
for(int i=0; i<10000; i++){
my_func_with_only_subscript(); Â Â
}
Â
// closing the cpu clock.
end = clock();
Â
// calculating time elapsed in secs.
cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
Â
printf("Time taken with subscript references::%f\n",cpu_time_used);
Â
// starting the cpu clock
start = clock();
Â
for(int i=0; i<10000; i++){
my_func_with_pointers();
}
Â
// closing the cpu clock.
end = clock();
Â
// calculating the time elapsed in array reference through pointers.
cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
Â
printf("Time taken with pointers::%f\n",cpu_time_used);
Â
return 0;
}
// function to reference the 2D array through subscripts.
void my_func_with_only_subscript()
{ //assuming that the usage of
int arr[15][5] = {0}; //square matrice is not mandatory
for(int j=0; j<5 ; j++){
for(int i=0; i<15; i++){
arr[i][j]; //no operation is done, only accessing the element
}
}
return;
}
// function to reference the 2D array through pointers.
void my_func_with_pointers()
{ //assuming that the usage of
int arr[15][5] = {0}; //square matrice is not mandatory
for(int j=0; j<5 ; j++){
for(int i=0; i<15; i++){
*(*(arr+i)+j); //no operation is done, only accessing the element
}
}
return;
}
/******************************************************************************************************************************/
OUTPUT
[Attached in the attachments]
