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

Compare the following two code segments. Do you think they are equivalent in per

ID: 3762420 • Letter: C

Question

Compare the following two code segments. Do you think they are equivalent in performance? Specify how you investigate this problem, and show the results of your experiments.

(Your experiments need to be thorough and factual. Issues to be considered may include, but are not limited to: What language do you use to do the tests? What values of row and column do you choose? Does the performance change when the values of row and column change? Does your computer put a limit on the size of the array you create? Based on the experiments, what insights do you get about your computer?)

for (i=0; i<row; i++) {

for (j=0; j<column; j++) {

    A[i][j] = 5;

}

for (j=0; j<column; j++) {

for (i=0; i<row; i++) {

    A[i][j] = 5;

}

Can someone help me with this, I trying to do it in Java language.

Code Segment 1 Code Segment 2

for (i=0; i<row; i++) {

for (j=0; j<column; j++) {

    A[i][j] = 5;

}

}

for (j=0; j<column; j++) {

for (i=0; i<row; i++) {

    A[i][j] = 5;

}

}

Explanation / Answer

Points to be noted that performance of code segments depends upon how much time the code is taking to execute ie. Time Complexity. In given code segments you are using 2 for loops one inside other. So for 1 iteration of outer loop full inner loop will iterate. Now time complexity will depend upon how many times loops wil get execute.

So for any value of rows and columns both code segment will execute same no of times. Suppose rows=4, columns=5

So for 1 row all 5 columns get updated in first code segement. So total 4*5=20

in Second Code Segment for 1 column all 4 rows get updated. So total 5*4=20

Performance also depends upon space complexity ie how much memory is using. but in given code segments so same rows and columns value both code segments will use same amount of memory.

Now considering following given points.

Performance depends upon what language you are using like if you are using low level language then it gives more performance than high levem languages and there is performance difference between compiled and intepreted language also.

Performance depends upon no of rows and columns because if you increase the value for memory will be uses and more resource will be used and more no times loop will execute.

Yes computer put limit on size of array depends upon computer processor gives you segmentation fault or memory dump if you provide very large size of array which your processor can not handle.

But if all of this conditions are same for both code segments then both code segments will give same performamce.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote