The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+1 vote
1.1k 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 in Compiler Design by Veteran (370k points) | 1.1k 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.2k 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)

Related questions

0 votes
0 answers
5


Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

44,463 questions
49,922 answers
165,479 comments
65,899 users