Velocity Reviews > Question of the day!!!

# Question of the day!!!

Anshum Kocher
Guest
Posts: n/a

 10-01-2011
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?

Willem
Guest
Posts: n/a

 10-01-2011
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

James Kuyper
Guest
Posts: n/a

 10-01-2011
On 10/01/2011 09:04 AM, 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?

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.
--
James Kuyper

Nick Keighley
Guest
Posts: n/a

 10-02-2011
On Oct 1, 2:04*pm, Anshum Kocher <(E-Mail Removed)> 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?

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.

Anshum Kocher
Guest
Posts: n/a

 10-03-2011
On Oct 2, 3:01*pm, Nick Keighley <(E-Mail Removed)>
wrote:
> On Oct 1, 2:04*pm, Anshum Kocher <(E-Mail Removed)> 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?

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

Eric Sosman
Guest
Posts: n/a

 10-03-2011
On 10/3/2011 7:17 AM, Anshum Kocher wrote:
>[...]
> 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

"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.

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)d

Jorgen Grahn
Guest
Posts: n/a

 10-04-2011
On Mon, 2011-10-03, Anshum Kocher wrote:
> On Oct 2, 3:01*pm, Nick Keighley <(E-Mail Removed)>
> wrote:
>> On Oct 1, 2:04*pm, Anshum Kocher <(E-Mail Removed)> 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?

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

Then don't trust whoever wrote that question. Unless it was a trick
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

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .

henry eshbaugh
Guest
Posts: n/a

 10-05-2011
On Oct 1, 9:04*am, Anshum Kocher <(E-Mail Removed)> 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?

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

Ben Bacarisse
Guest
Posts: n/a

 10-05-2011
henry eshbaugh <(E-Mail Removed)> writes:

> On Oct 1, 9:04Â*am, Anshum Kocher <(E-Mail Removed)> 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?

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

--
Ben.

Keith Thompson
Guest
Posts: n/a

 10-05-2011
henry eshbaugh <(E-Mail Removed)> writes:
> On Oct 1, 9:04Â*am, Anshum Kocher <(E-Mail Removed)> 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?

>
> 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).

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"