Question of the day!!!

A

Anshum Kocher

Code A:
float a=5 ,b=10;
float c=a+(b-a)/a+(b-a)*(b-a);
//End of code A

Code B:
float a=5,b=10;
float d=b-a;
float c=a+d/a+d*d;

Question:

Which code segment A or B takes more memory and which ones take more
memory?
 
W

Willem

Anshum Kocher wrote:
) Code A:
) float a=5 ,b=10;
) float c=a+(b-a)/a+(b-a)*(b-a);
) //End of code A
)
) Code B:
) float a=5,b=10;
) float d=b-a;
) float c=a+d/a+d*d;
)
) Question:
)
) Which code segment A or B takes more memory and which ones take more
) memory?

Your question doesn't make sense. Even ignoring the poor wording.
In any case, any decent compiler will generate the same code for both,
assuming 'd' isn't used somewhere else.


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
J

James Kuyper

Code A:
float a=5 ,b=10;
float c=a+(b-a)/a+(b-a)*(b-a);
//End of code A

Code B:
float a=5,b=10;
float d=b-a;
float c=a+d/a+d*d;

Question:

Which code segment A or B takes more memory and which ones take more
memory?

That depends entirely upon the compiler and the context. Assuming that
the above is the only difference between two otherwise identical
programs, most decent compilers should generate essentially equivalent
code for both cases.
 
N

Nick Keighley

Code A:
float a=5 ,b=10;
float c=a+(b-a)/a+(b-a)*(b-a);
//End of code A

Code B:
float a=5,b=10;
float d=b-a;
float c=a+d/a+d*d;

Question:

Which code segment A or B takes more memory and which ones take more
memory?

you do know you asked the same question twice? Why don't you just
measure it, if you care. I suspect the answer is "not much" or "none
at all". For the record I'd use the second version as I don't like
repeating myself. Oh and I'd prefer double to float.
 
A

Anshum Kocher

you do know you asked the same question twice? Why don't you just
measure it, if you care. I suspect the answer is "not much" or "none
at all". For the record I'd use the second version as I don't like
repeating myself. Oh and I'd prefer double to float.

It was more of a typing error....the actual question was
Which code segment A or B takes more memory and which ones take more
time?
and it was a question asked to me in a written examination...
 
E

Eric Sosman

[...]
It was more of a typing error....the actual question was
Which code segment A or B takes more memory and which ones take more
time?
and it was a question asked to me in a written examination...

If the written examination concerned the C language, the
correct answers would have been

"It depends on the implementation."
and
"It depends on the implementation."

If the examination concerned a specific implementation of C,
the correct answers would have been

"It depends on the context."
and
"It depends on the context."

If the examination concerned a specific implementation of C
in the context of a specific complete program, the correct answers
would have been

"I'll have to measure it."
and
"Who cares?"

Seriously.
 
J

Jorgen Grahn

It was more of a typing error....the actual question was
Which code segment A or B takes more memory and which ones take more
time?
and it was a question asked to me in a written examination...

Then don't trust whoever wrote that question. Unless it was a trick
question or had additional restrictions, that person doesn't seem to
understand C (or programming in general).

Both A and B can be optimized away to three constants -- or less if
the rest of the code doesn't use all of them.

/Jorgen
 
H

henry eshbaugh

Code A:
float a=5 ,b=10;
float c=a+(b-a)/a+(b-a)*(b-a);
//End of code A

Code B:
float a=5,b=10;
float d=b-a;
float c=a+d/a+d*d;

Question:

Which code segment A or B takes more memory and which ones take more
memory?

Independent of architecture and compiler, B takes up more space.
 
B

Ben Bacarisse

henry eshbaugh said:
Independent of architecture and compiler, B takes up more space.

Why do you say that? Part of my question is just that I know what
"takes up more space" means for code fragments like this.
 
K

Keith Thompson

henry eshbaugh said:
Independent of architecture and compiler, B takes up more space.

Evidence?

Yes, I know B defines more objects than A does; that won't necessarily
show up in the generated code.

And in fact "gcc -O3" generates *exactly* the same code for both
(with a small wrapper program that prints the value of c).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top