while Vs for loop

Discussion in 'C Programming' started by Mahesh Kumar Reddy.R, Sep 27, 2004.

  1. Hi

    Can any body resolve this..
    In what cases one of the loop constructs better than other interms of
    speed , space and any other (redability).

    thanks
    mahesh
    Mahesh Kumar Reddy.R, Sep 27, 2004
    #1
    1. Advertising

  2. On 27 Sep 2004 13:51:19 -0700, in comp.lang.c ,
    (Mahesh Kumar Reddy.R) wrote:

    >In what cases one of the loop constructs better than other interms of
    >speed , space and any other (redability).


    Homework question.....

    Other than poor optimisation by the compiler? :)

    While and for have different checking conditions, this might make a
    difference, depending on the complexity of what you're checking. Also a
    do...while loop checks its condition at the end. This might save you a
    check. See if you can work out where and when.
    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>


    ----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
    ---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
    Mark McIntyre, Sep 27, 2004
    #2
    1. Advertising

  3. Mahesh Kumar Reddy.R

    Default User Guest

    Mahesh Kumar Reddy.R wrote:

    > Hi
    >
    > Can any body resolve this..
    > In what cases one of the loop constructs better than other interms of
    > speed , space and any other (redability).



    A better idea is for you to learn some C programming. After you've
    written a significant amount of code, you'll pretty know the answer.




    Brian Rodenborn
    Default User, Sep 27, 2004
    #3
  4. Mahesh Kumar Reddy.R

    Mike Wahler Guest

    "Mahesh Kumar Reddy.R" <> wrote in message
    news:...
    > Hi
    >
    > Can any body resolve this..
    > In what cases one of the loop constructs better than other


    When it more closely models the problem being solved than
    the others.

    > interms of
    > speed , space



    'Speed' and 'space' are QoI (quality of implementation)
    issues, not defined by the language.

    >and any other (redability).


    See my first answer above.

    -Mike
    Mike Wahler, Sep 27, 2004
    #4
  5. Mahesh Kumar Reddy.R

    CBFalconer Guest

    "Mahesh Kumar Reddy.R" wrote:
    >
    > Can any body resolve this..
    > In what cases one of the loop constructs better than other
    > interms of speed , space and any other (redability).


    No.

    --
    Some useful references:
    <http://www.ungerhu.com/jxh/clc.welcome.txt>
    <http://www.eskimo.com/~scs/C-faq/top.html>
    <http://benpfaff.org/writings/clc/off-topic.html>
    <http://anubis.dkuug.dk/jtc1/sc22/wg14/www/docs/n869/> (C99)
    <http://www.dinkumware.com/refxc.html> C-library
    CBFalconer, Sep 28, 2004
    #5
  6. Hi

    I checked the following two programs with DDD(digital dispaly
    debugger on linux). In for loop, the index variable i is always kept
    in EAX
    register but in while loop it was kept in EAX register only at the
    time where the actual use of index variable(i.e at the time of
    incrementing index
    variable) and after the increment some other variable using EAX
    register. In next iteration variable i is stored back to EAX.

    There may be a case that the compiler(will told ahead of time that
    increment index variable with each iteration)knows ,ahead of the time,
    that the index variable in for loop will be incremented with each
    iteration. where as in While loop the incrementing index variable is
    known at end of the body the while.
    And the compiler won't tell that this index variable will always be
    incremented
    with each iteration.



    So from the above experiment for loop executes faster than while loop.
    The above conclusion is based on the assumtion that the register
    access is
    faster than memory access.

    --------------------------------------------------------------------
    for.c
    -----------------------------------------------------------------------
    #include <stdio.h>


    int main()
    {
    int i,n=10,sum=0;
    for(i = 0; i < n; i++)
    {
    sum += i;
    }

    }
    ----------------------------------------------------------------------------
    while.c
    --------------------------------------------------------------------------

    #include <stdio.h>


    int main()
    {
    int i,n=10,sum=0;
    i=0;
    while(i < n)
    {
    sum += i;
    i++;
    }

    }
    ~--------------------------------------------------------------------------------------




    CBFalconer <> wrote in message news:<>...
    > "Mahesh Kumar Reddy.R" wrote:
    > >
    > > Can any body resolve this..
    > > In what cases one of the loop constructs better than other
    > > interms of speed , space and any other (redability).

    >
    > No.
    Mahesh Kumar Reddy.R, Sep 29, 2004
    #6
  7. Mahesh Kumar Reddy.R

    CBFalconer Guest

    *** Rude top-posting corrected ***

    "Mahesh Kumar Reddy.R" wrote:
    > CBFalconer <> wrote:
    >> "Mahesh Kumar Reddy.R" wrote:
    >>>
    >>> Can any body resolve this..
    >>> In what cases one of the loop constructs better than other
    >>> interms of speed , space and any other (redability).

    >>
    >> No.

    >
    > I checked the following two programs with DDD(digital dispaly
    > debugger on linux). In for loop, the index variable i is always
    > kept in EAX register but in while loop it was kept in EAX
    > register only at the time where the actual use of index
    > variable(i.e at the time of incrementing index variable) and
    > after the increment some other variable using EAX register. In
    > next iteration variable i is stored back to EAX.


    .... snip pointless code ...

    So? A MAC, or a CP/M machine, or a VAX, etc. don't have an EAX
    register. Your experiment means absolutely nothing in the world
    of portable C. The thing that counts is the C standard.

    Do not top-post. It is rude, and not generally tolerated here.
    Your answer belongs after (or intermixed with) the material to
    which you are replying, with anything not germane to your reply
    snipped out.

    --
    A: Because it fouls the order in which people normally read text.
    Q: Why is top-posting such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    CBFalconer, Sep 29, 2004
    #7
  8. Mahesh Kumar Reddy.R

    Chris Torek Guest

    In article <>,
    >I checked the following two programs with DDD(digital dispaly
    >debugger on linux). In for loop, the index variable i is always kept
    >in EAX
    >register but in while loop it was kept in EAX register only at the
    >time where the actual use of index variable(i.e at the time of
    >incrementing index
    >variable) and after the increment some other variable using EAX
    >register. In next iteration variable i is stored back to EAX.


    [snippage]

    >So from the above experiment for loop executes faster than while loop.
    >The above conclusion is based on the assumtion that the register
    >access is
    >faster than memory access.


    As I think Dan Pop noted, that only tests one particular implementation;
    the results cannot be generalized to other implementations. I find
    it curious, however, given what I know about the internals of gcc,
    that you found any difference at all. Here are your two programs
    (with some whitespace compression for news-posting-reasons, though
    the versions I compiled did not even have that), plus the difference
    between the results of compiling them.

    Both were compiled with and without optimization, using gcc (3.2.3)
    on Linux (Red Hat):

    > for.c
    >#include <stdio.h>
    >
    >int main() {
    > int i,n=10,sum=0;
    > for(i = 0; i < n; i++) { sum += i; }
    >}
    > while.c
    >#include <stdio.h>
    >
    >int main()
    >{
    > int i,n=10,sum=0;
    > i=0; while(i < n) { sum += i; i++; }
    >}


    % cd /tmp
    % cc -S for.c while.c
    % diff for.s while.s
    1c1
    < .file "for.c"
    ---
    > .file "while.c"

    18c18
    < jl .L5
    ---
    > jl .L4

    20c20
    < .L5:
    ---
    > .L4:

    % cc -S -O4 -mregparm=3 -fomit-frame-pointer for.c while.c
    % diff for.s while.s
    1c1
    < .file "for.c"
    ---
    > .file "while.c"

    13c13
    < .L6:
    ---
    > .L5:

    15c15
    < jns .L6
    ---
    > jns .L5


    As this shows, the only difference is the name of the file in
    the .file directive, and the name of the local label controlling
    the loop. In the optimized code, the actual loop is just:

    .Ln: decl %eax; jns .Ln

    Since the sum is not used, the variable that holds it has been
    discarded, and the loop has been transformed from "i counts up from
    0 to 9 inclusive" to "i counts down from 9 to 0 inclusive".
    --
    In-Real-Life: Chris Torek, Wind River Systems
    Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
    email: forget about it http://web.torek.net/torek/index.html
    Reading email is like searching for food in the garbage, thanks to spammers.
    Chris Torek, Sep 30, 2004
    #8
    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. Steven

    while loop in a while loop

    Steven, Mar 24, 2005, in forum: Java
    Replies:
    5
    Views:
    2,206
    Tim Slattery
    Mar 30, 2005
  2. Daniel Pitts
    Replies:
    14
    Views:
    1,209
    Patricia Shanahan
    Dec 23, 2006
  3. Uday Bidkar
    Replies:
    4
    Views:
    471
    =?ISO-8859-15?Q?Juli=E1n?= Albo
    Dec 12, 2006
  4. Roedy Green
    Replies:
    3
    Views:
    406
    Mike Schilling
    Sep 13, 2008
  5. Isaac Won
    Replies:
    9
    Views:
    342
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page