Objective To gain insight into how the software design can s
Objective: To gain insight into how the software design can significantly impact execution time because of how the hardware and operating system actually implement the program.
Below are two versions of code that copies the entire contents of one matrix to the other. Implement each of the routines. Then, create a source array populated with random numbers. Make destination arrays of the same size and copy the contents of the source to the destination arrays and measure the time it takes for each copy routine to execute. To gain further insight, I recommend that you vary the size of the matrices to see where the execution times become similar. Generate a write up that summarizes your experimental results and an explanation of why you think the execution times may vary. (We will cover this topic in detail, but this experiment will provide you an appreciation of the impact.)
Solution
#include<stdlib.h>
#include<stdio.h>
#include<time.h>
#include<conio.h>
void copyij(int src[][200],int dst[][200])
{
int i,j;
for(i=0;i<200;i++)
for(j=0;j<200;j++)
dst[i][j]=src[i][j];
}
void copyji(int src[][200],int dst[200][200])
{
int i,j;
for(j=0;j<200;j++)
for(i=0;i<200;i++)
dst[i][j]=src[i][j];
}
int main()
{
int src[200][200];
int dst[200][200];
int i,j;
clock_t start,stop;
srand(time(NULL));
for(i=0;i<200;i++)
for(j=0;j<200;j++)
src[i][j]=rand()/2000;
copyij(src,dst);
copyji(src,dst);
return 0;
}
Surely the version1 will take less time. as It accessed the data row major order .
I a computer physicalluy the elements are accessed row wise. and when a byte is one element is requested,A bock of data(depending upon bocl size) is transmitted from RAM to cache. If a block comntains 2048 element, 2048 elelmet are transmitten to cache on a single request.So next element in same row doesnot need RAM access.They are already availed in cache.
When elment od 2nd row is accessed then another RAM access is needed, So a total of 2048 RAM access are needed in version 1.
But in version2, The element are accessed in column mojor order.
Here on requesting 1st elelemt on 1st row, All elements in this row are transmitted to Cache,But only is request ed data(src[0][0] is used. Then 2ns element in 1st row row is requested. Again a block of data(2028 elements) are transmitted, but we use one.
In this fashion a total of 2048X2048 RAM access are needed. So this version (version2) takes more time.

