![]() |
Question of the day!!!
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? |
Re: Question of the day!!!
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 |
Re: Question of the day!!!
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 |
Re: Question of the day!!!
On Oct 1, 2:04*pm, Anshum Kocher <email.ans...@gmail.com> 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. |
Re: Question of the day!!!
On Oct 2, 3:01*pm, Nick Keighley <nick_keighley_nos...@hotmail.com>
wrote: > On Oct 1, 2:04*pm, Anshum Kocher <email.ans...@gmail.com> 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... |
Re: Question of the day!!!
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 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. -- Eric Sosman esosman@ieee-dot-org.invalid |
Re: Question of the day!!!
On Mon, 2011-10-03, Anshum Kocher wrote:
> On Oct 2, 3:01*pm, Nick Keighley <nick_keighley_nos...@hotmail.com> > wrote: >> On Oct 1, 2:04*pm, Anshum Kocher <email.ans...@gmail.com> 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 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 -- // Jorgen Grahn <grahn@ Oo o. . . \X/ snipabacken.se> O o . |
Re: Question of the day!!!
On Oct 1, 9:04*am, Anshum Kocher <email.ans...@gmail.com> 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. |
Re: Question of the day!!!
henry eshbaugh <henryeshbaugh@gmail.com> writes:
> On Oct 1, 9:04Â*am, Anshum Kocher <email.ans...@gmail.com> 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. |
Re: Question of the day!!!
henry eshbaugh <henryeshbaugh@gmail.com> writes:
> On Oct 1, 9:04Â*am, Anshum Kocher <email.ans...@gmail.com> 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) kst-u@mib.org <http://www.ghoti.net/~kst> "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister" |
| All times are GMT. The time now is 10:52 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.