+1 vote
1.6k views

Pankaj and Mythili were both asked to write the code to evaluate the following expression:
$$a - b + c/(a-b) + (a-b)^2$$
Pankaj writes the following code statements (Code A):

print (a-b) + c/(a-b) + (a-b)*(a-b)

Mythili writes the following code statements (Code B):

d = (a-b)
print d + c/d + d*d

If the time taken to load a value in a variable, for addition, multiplication or division between two operands is same, which of the following is true?

Option 1 : Code A uses lesser memory and is slower than Code B
Option 2 : Code A uses lesser memory and is faster than Code B
Option 3 : Code A uses more memory and is faster than Code B
Option 4 : Code A uses more memory and is slower than Code B

Like

asked | 1.6k views

## 2 Answers

+5 votes
Best answer

Option 1 : Code A uses lesser memory and is slower than Code B

In code A, three variables are used a, b and c to store values. The computation of a-b is repetitive and is performed 4 times in print statement requires more time compared to the computation of print statement of code B, because of the use of additional variable d, to store the computed value of a-b. The print statement of code B replaces d with the already computed value of a-b.

answered by Loyal (6.1k points)
selected by
0
but the second part consists of two instruction fetch cycle.

why shouldn't we take that into account?
–1 vote
their is the second option is true.

Option 2 : Code A uses lesser memory and is faster than Code B

If we are define 3 variable then low memory used. and if go with the 4 variable then every time d=(a-b)

firstly. so the code becomes slow. so the I m go with Option 2 : Code A uses lesser memory and is faster than Code B.
answered by (9 points)

0 votes
0 answers
1
+2 votes
1 answer
2
+1 vote
1 answer
3
+1 vote
1 answer
4
0 votes
0 answers
5
0 votes
1 answer
6
+14 votes
3 answers
7