[2 pts] Compare the following two code segments. Do you think they are equivalen
ID: 3771325 • Letter: #
Question
[2 pts] 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.
(By now, you should have learnt enough to know how to conduct vigorous 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?)
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;
}
}
Read the article “Memory layout of multi-dimensional arrays” by Bendersky (http://eli.thegreenplace.net/2015/memory-layout-of-multi-dimensional-arrays/).
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
Result of both codes is same, but while considering the performance they are different since the 2d row major and 2d column major play important role on the performance and thus there is performance differnce in the both codes.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.