Question of the day!!!

Discussion in 'C Programming' started by Anshum Kocher, Oct 1, 2011.

  1. 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?
     
    Anshum Kocher, Oct 1, 2011
    #1
    1. Advertising

  2. Anshum Kocher

    Willem Guest

    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
     
    Willem, Oct 1, 2011
    #2
    1. Advertising

  3. Anshum Kocher

    James Kuyper Guest

    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
     
    James Kuyper, Oct 1, 2011
    #3
  4. On Oct 1, 2:04 pm, 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?


    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.
     
    Nick Keighley, Oct 2, 2011
    #4
  5. On Oct 2, 3:01 pm, Nick Keighley <>
    wrote:
    > On Oct 1, 2:04 pm, 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?

    >
    > 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...
     
    Anshum Kocher, Oct 3, 2011
    #5
  6. Anshum Kocher

    Eric Sosman Guest

    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
    d
     
    Eric Sosman, Oct 3, 2011
    #6
  7. Anshum Kocher

    Jorgen Grahn Guest

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

    >>
    >> 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 .
     
    Jorgen Grahn, Oct 4, 2011
    #7
  8. On Oct 1, 9: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?


    Independent of architecture and compiler, B takes up more space.
     
    henry eshbaugh, Oct 5, 2011
    #8
  9. henry eshbaugh <> writes:

    > On Oct 1, 9: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?

    >
    > 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.
     
    Ben Bacarisse, Oct 5, 2011
    #9
  10. henry eshbaugh <> writes:
    > On Oct 1, 9: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?

    >
    > 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) <http://www.ghoti.net/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Oct 5, 2011
    #10
  11. Anshum Kocher

    Noob Guest

    henry eshbaugh wrote:

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


    I count 48 characters for both code fragments.
    Ergo, they take the same space.
     
    Noob, Oct 5, 2011
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VB Programmer
    Replies:
    2
    Views:
    432
    Alan Lambert
    Nov 4, 2004
  2. =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=

    calander question about getting the day of the seected date

    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=, May 31, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    1,012
    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=
    Jun 2, 2005
  3. Andy
    Replies:
    1
    Views:
    736
  4. Replies:
    2
    Views:
    280
  5. Rob Redmon

    Convert day of year to month, day

    Rob Redmon, Apr 4, 2008, in forum: Ruby
    Replies:
    7
    Views:
    314
    Alex Wayne
    Apr 4, 2008
Loading...

Share This Page