Ravi and Rupali are asked to write a program to sum the rows of 2X2 matrices stored in the array A.
Ravi writes the following code (Code A):
for n = 0 to 1 sumRow1[n] = A[n] + A[n] end
Rupali writes the following code (Code B):
sumRow1 = A + A sumRow1 = A + A
Comment upon these codes (Assume no loop unrolling done by compiler) ?
A. Code a will execute faster than b.
B. Code b will execute faster than a.
C. Code a is logically incorrect.
D. Code b is logically incorrect
Answer : B. Code b will execute faster than a
- Both codes are taking two steps of operation, so same complexity.
- But if we consider the overhead of looping (as it takes time to increment counter) then code b will be faster.