while Vs for loop

  • Thread starter Mahesh Kumar Reddy.R
  • Start date
M

Mahesh Kumar Reddy.R

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
 
M

Mark McIntyre

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

Default User

Mahesh said:
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
 
M

Mike Wahler

Mahesh Kumar Reddy.R said:
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
 
C

CBFalconer

M

Mahesh Kumar Reddy.R

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++;
}

}
~--------------------------------------------------------------------------------------
 
C

CBFalconer

*** Rude top-posting corrected ***

Mahesh Kumar Reddy.R said:
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.
 
C

Chris Torek

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)
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
---
20c20
< .L5:
---
% 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:
---
15c15
< jns .L6
---

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

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top