in Compiler Design retagged by
95,739 views
4 votes
4 votes

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?

  1. Code A uses lesser memory and is slower than Code B 
  2. Code A uses lesser memory and is faster than Code B 
  3. Code A uses more memory and is faster than Code B 
  4. Code A uses more memory and is slower than Code B
in Compiler Design retagged by
by
95.7k views

2 Answers

9 votes
9 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.

selected by

1 comment

but the second part consists of two instruction fetch cycle.

why shouldn't we take that into account?
0
0
–1 vote
–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.

Related questions