Consider these two functions and two statements S1 and S2 about them.
int work1(int *a, int i, int j)
int x = a[i+2];
a[j] = x+1;
return a[i+2] - 3;
int work2(int *a, int i, int j)
int t1 = i+2;
int t2 = a[t1];
a[j] = t2+1;
return t2 - 3;
S1: The transformation form work1 to work2 is valid, i.e., for any program state and input arguments, work2 will compute the same output and have the same effect on program state as work1
S2: All the transformations applied to work1 to get work2 will always improve the performance (i.e reduce CPU time) of work2 compared to work1
Code hoisting doesn't reduce time.
important difference to notice in this question is
return a[i+2] - 3; in work1
return t2 - 3; in work2
so in first one we are directly doing calculation on array value.
where as in second one we are storing value somewhere.
So there are chances that in first one value in array can get modified .
And now we have to find that combination for which it gets modified
Consider an array a = 1 2 3 4 5 and condition i + 2 =j. Lets take i =0 and j =2 for this example.
x = a[0+2] = 3
a = 3 + 1 = 4; which means a = 1 2 4 4 5
return a[0+2] - 3 = 4 -3 = 1
t1 = 0 + 2 = 2
t2 = a = 3
a = 3 + 1 = 4, which means a = 1 2 4 4 5 again
return t2 - 3 = 3 -3 =0
Hence S1 is false when i + 2 =j. S2 will also be false, since we cant explicitly say the performance of work2 will always be better than work1.
Hence answer is A
@Anmol 300 books will be dispatched this month...