473,386 Members | 1,799 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

while Vs for loop

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
Nov 14 '05 #1
7 3184
On 27 Sep 2004 13:51:19 -0700, in comp.lang.c , ma***********@gmail.com
(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 =---
Nov 14 '05 #2
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
Nov 14 '05 #3
"Mahesh Kumar Reddy.R" <ma***********@gmail.com> wrote in message
news:f4**************************@posting.google.c om...
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
Nov 14 '05 #4
"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
Nov 14 '05 #5
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 <cb********@yahoo.com> wrote in message news:<41***************@yahoo.com>...
"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.

Nov 14 '05 #6
*** Rude top-posting corrected ***

"Mahesh Kumar Reddy.R" wrote:
CBFalconer <cb********@yahoo.com> 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?
Nov 14 '05 #7
In article <f4**************************@posting.google.com >,
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.
Nov 14 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: jsaul | last post by:
Hi there, wouldn't it be useful to have a 'while' conditional in addition to 'if' in list comprehensions? foo = for i in bar: if len(i) == 0: break foo.append(i)
33
by: Diez B. Roggisch | last post by:
Hi, today I rummaged through the language spec to see whats in the for ... else: for me. I was sort of disappointed to learn that the else clauses simply gets executed after the loop-body -...
9
by: Ben | last post by:
I have two 'Do While Not' statements, that are getting information from the same recordset. If I comment out the first one I can get the results for the second one, and vice-versa. Why is this...
36
by: invni | last post by:
I have a nested while. How do I go from the inner while to the beginning of the outer while? Can this be done without using goto? while_1() { some codes here while_2() { if true go to the...
6
by: John Pass | last post by:
What is the difference between a While and Do While/Loop repetition structure. If they is no difference (as it seems) why do both exist?
12
by: Howard | last post by:
Hello everyone (total VB.NET beginner here), I'm reading the "SAMS Teach Yourself VB.NET In 21 Days" book, and came across an exercise that I can't get to work. The exercise asks that you create...
16
by: Claudio Grondi | last post by:
Sometimes it is known in advance, that the time spent in a loop will be in order of minutes or even hours, so it makes sense to optimize each element in the loop to make it run faster. One of...
10
by: rohitjogya | last post by:
Can anyone tell me the difference bet for loop and while loop execution? ____________________ for (i=0 ; i<10 ; i++) ; /* Do nothing*/ print i; ___________________ i=0;
5
by: Alex | last post by:
Hi I just want to clear something up in my head with while loops and exceptions. I'm sure this will probably be a no brainer for most. Check this simple pseudo-code out (vb.net): ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.