Velocity Reviews > infinite loop

# infinite loop

karthikbalaguru
Guest
Posts: n/a

 01-28-2009
On Jan 24, 5:59 am, Martin Ambuhl <(E-Mail Removed)> wrote:
> kenny wrote:
> > i want to write infinite loop

>
> > 1. can do in c language ?

>
> while (1) {} /* one way */
>
> do {} while(1); /* another way */
>
> for (; {} /* a third way */
>
> foo: goto foo; /* a fourth way */
>

But, why does C provide some many
ways of infinite loop ?

Which one of the above methods consume
less cycles ?

Karthik Balaguru

karthikbalaguru
Guest
Posts: n/a

 01-28-2009
On Jan 28, 10:54 am, Richard Heathfield <(E-Mail Removed)> wrote:
> karthikbalaguru said:
>
>
>
> > On Jan 24, 5:59 am, Martin Ambuhl <(E-Mail Removed)> wrote:
> >> kenny wrote:
> >> > i want to write infinite loop

>
> >> > 1. can do in c language ?

>
> >> while (1) {} /* one way */

>
> >> do {} while(1); /* another way */

>
> >> for (; {} /* a third way */

>
> >> foo: goto foo; /* a fourth way */

>
> > But, why does C provide some many
> > ways of infinite loop ?

>
> Imagine that you are a craftsman in a workshop that contains many
> tools. Someone comes in and asks you how many different ways you
> can think of to insert a woodscrew into an offcut.
>
> First, you demonstrate how to do this with a manual screwdriver.
>
> Then you do the same thing, this time with an electric screwdriver.
>
> Then you show that it can be done with a hammer, too, albeit not
> quite so neatly.
>
> Fourthly, you cut a small recess in the wood with a hammer and
> chisel and place a screw into the recess.
>
> Fifthly, you drill a hole wider than the screw and drop the screw
> into the hole. (Again, you can do this both manually and
> electrically.)
>
> You may be able to think of some other ways to do this, too.
>
> But, says your visitor, WHY does carpentry provide so many ways to
> do this?
>
> > Which one of the above methods consume
> > less cycles ?

>
> That's the wrong question - partly because it makes little if any
> difference, and partly because the answer would vary from machine
> to machine,

Considering a x86 machine, which one will be faster (Less Cycles) ?
Any ideas ?

Karthik Balaguru

karthikbalaguru
Guest
Posts: n/a

 01-28-2009
On Jan 28, 10:54 am, Richard Heathfield <(E-Mail Removed)> wrote:
> karthikbalaguru said:
>
>
>
> > On Jan 24, 5:59 am, Martin Ambuhl <(E-Mail Removed)> wrote:
> >> kenny wrote:
> >> > i want to write infinite loop

>
> >> > 1. can do in c language ?

>
> >> while (1) {} /* one way */

>
> >> do {} while(1); /* another way */

>
> >> for (; {} /* a third way */

>
> >> foo: goto foo; /* a fourth way */

>
> > But, why does C provide some many
> > ways of infinite loop ?

>
> Imagine that you are a craftsman in a workshop that contains many
> tools. Someone comes in and asks you how many different ways you
> can think of to insert a woodscrew into an offcut.
>
> First, you demonstrate how to do this with a manual screwdriver.
>
> Then you do the same thing, this time with an electric screwdriver.
>
> Then you show that it can be done with a hammer, too, albeit not
> quite so neatly.
>
> Fourthly, you cut a small recess in the wood with a hammer and
> chisel and place a screw into the recess.
>
> Fifthly, you drill a hole wider than the screw and drop the screw
> into the hole. (Again, you can do this both manually and
> electrically.)
>
> You may be able to think of some other ways to do this, too.
>
> But, says your visitor, WHY does carpentry provide so many ways to
> do this?
>
> > Which one of the above methods consume
> > less cycles ?

>
> That's the wrong question - partly because it makes little if any
> difference, and partly because the answer would vary from machine
> to machine,

Considering a x86 machine, which one will be faster (Less Cycles) ?
and Which one will consume more memory ?
Any ideas ?

Karthik Balaguru

karthikbalaguru
Guest
Posts: n/a

 01-28-2009
On Jan 28, 12:54 pm, Richard Heathfield <(E-Mail Removed)> wrote:
> karthikbalaguru said:
>
>
>
> > On Jan 28, 10:54 am, Richard Heathfield <(E-Mail Removed)>
> > wrote:
> >> karthikbalaguru said:

>
> <snip>
>
> >> > Which one of the above methods [of programming an infinite
> >> > loop] consume less cycles ?

>
> >> That's the wrong question - partly because it makes little if any
> >> difference, and partly because the answer would vary from machine
> >> to machine,

>
> > Considering a x86 machine, which one will be faster (Less Cycles)
> > ? Any ideas ?

>
> It's still the wrong question.
>
> Consider this: either the loop body will be doing a lot of work (in
> which case the tiny cost of looping, *however* you do it, will be
> insignificant), or the loop body will not be doing a lot of work
> (in which case the overhead doesn't matter anyway, because there's
> plenty of capacity).
>

Thx for that info !!
But, i am really interested to know considering
the loop body does not do anything. (It is just an infinite loop
without body.)

I am intested to know this because various forms of
programming an infinite loop is supported .

Also, are there any specific scenarios in which
those methods should be used ?

Any ideas ?

Karthik Balaguru

Keith Thompson
Guest
Posts: n/a

 01-28-2009
karthikbalaguru <(E-Mail Removed)> writes:
> On Jan 24, 5:59 am, Martin Ambuhl <(E-Mail Removed)> wrote:
>> kenny wrote:
>> > i want to write infinite loop

>>
>> > 1. can do in c language ?

>>
>> while (1) {} /* one way */
>>
>> do {} while(1); /* another way */
>>
>> for (; {} /* a third way */
>>
>> foo: goto foo; /* a fourth way */
>>

>
> But, why does C provide some many
> ways of infinite loop ?

C doesn't provide any construct that's specifically for infinite
loops. It provides a number of distinct looping constructs, each with
its own uses. (You could get by with just one, but the different
forms are more convenient in different circumstances.) And, not
surprisingly, each of the forms of loop can be specialized to form an
infinite loop.

> Which one of the above methods consume
> less cycles ?

As Richard explained at some length, you're asking the wrong question.
exactly the same number of cycles. A decent compiler will probably
generate exactly the same code for all of them.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

nick_keighley_nospam@hotmail.com
Guest
Posts: n/a

 01-28-2009
On 28 Jan, 06:38, karthikbalaguru <(E-Mail Removed)> wrote:
> On Jan 28, 10:54 am, Richard Heathfield <(E-Mail Removed)> wrote:
> > karthikbalaguru said:
> > > On Jan 24, 5:59 am, Martin Ambuhl <(E-Mail Removed)> wrote:
> > >> kenny wrote:

> > >> > i want to write infinite loop

>
> > >> > 1. can do in c language ?

>
> > >> while (1) {} /* one way */

>
> > >> do {} while(1); /* another way */

>
> > >> for (; {} /* a third way */

>
> > >> foo: goto foo; */* a fourth way */

>
> > > But, why does C provide some many
> > > ways of infinite loop ?

>
> > Imagine that you are a craftsman in a workshop that contains many
> > tools. Someone comes in and asks you how many different ways you
> > can think of to insert a woodscrew into an offcut.

>
> > First, you demonstrate how to do this with a manual screwdriver.

>
> > Then you do the same thing, this time with an electric screwdriver.

>
> > Then you show that it can be done with a hammer, too, albeit not
> > quite so neatly.

>
> > Fourthly, you cut a small recess in the wood with a hammer and
> > chisel and place a screw into the recess.

>
> > Fifthly, you drill a hole wider than the screw and drop the screw
> > into the hole. (Again, you can do this both manually and
> > electrically.)

>
> > You may be able to think of some other ways to do this, too.

>
> > But, says your visitor, WHY does carpentry provide so many ways to
> > do this?

>
> > > Which one of the above methods consume
> > > less cycles ?

>
> > That's the wrong question - partly because it makes little if any
> > difference, and partly because the answer would vary from machine
> > to machine,

>
> Considering a x86 machine, which one will be faster (Less Cycles) ?
> and Which one will consume more memory ?
> Any ideas ?

its been explained to you why this is a bad question.
If you *really* want to know (and I can think of
no good reason) then write a program and look at the assembler
generated. You will find the code very similar in all cases.

Bartc
Guest
Posts: n/a

 01-28-2009

"karthikbalaguru" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Jan 24, 5:59 am, Martin Ambuhl <(E-Mail Removed)> wrote:
>> kenny wrote:
>> > i want to write infinite loop

>>
>> > 1. can do in c language ?

>>
>> while (1) {} /* one way */
>>
>> do {} while(1); /* another way */
>>
>> for (; {} /* a third way */
>>
>> foo: goto foo; /* a fourth way */
>>

>
> But, why does C provide some many
> ways of infinite loop ?

Why does it provide so many ways of looping?

>
> Which one of the above methods consume
> less cycles ?

For an /infinite/ loop, will it really make any difference? Everyone will

--
Bartc

Martin Ambuhl
Guest
Posts: n/a

 01-28-2009
karthikbalaguru wrote:

> But, i am really interested to know considering
> the loop body does not do anything. (It is just an infinite loop
> without body.)
>
> I am intested to know this because various forms of
> programming an infinite loop is supported .
>
> Also, are there any specific scenarios in which
> those methods should be used ?
>
> Any ideas ?

This is just silly. If a loop is truly infinite it has an infinite
number of cycles performed in an infinite amount of time. So constuct 1
has infinity[1] cycles / infinity[1] time and construct 2 has
infinity[2] cycles / infinity[2] time.

Harold Aptroot
Guest
Posts: n/a

 01-28-2009
"karthikbalaguru" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
(snipped far too long quote)
>
> Considering a x86 machine, which one will be faster (Less Cycles) ?
> Any ideas ?
>
> Karthik Balaguru

Any real compiler is likely to optimize all those kinds of loops into:

foo: goto foo;

But in case it won't (maybe a very dumb/old compiler), writing that directly
shouldn't result in more than just a jump. There is no guarantee though, the
compiler is free to (for example) insert a whole load of nops between the
label and the goto (or anywhere else). I would trust most C compilers to be
"sane" in addition to "conforming to the C standard" though.

Eric Sosman
Guest
Posts: n/a

 01-28-2009
karthikbalaguru wrote:
> On Jan 24, 5:59 am, Martin Ambuhl <(E-Mail Removed)> wrote:
>> kenny wrote:
>>> i want to write infinite loop
>>> 1. can do in c language ?

>> while (1) {} /* one way */
>>
>> do {} while(1); /* another way */
>>
>> for (; {} /* a third way */
>>
>> foo: goto foo; /* a fourth way */
>>

>
> But, why does C provide some many
> ways of infinite loop ?
>
> Which one of the above methods consume
> less cycles ?

All of them consume more cycles than your machine can execute.
Aleph-null is aleph-null is aleph-null, and the Universe will rot
away before any of these loops completes.

(Unless you've got a DeathStation 9000, of course. The DS9K
is so fast it can complete a well-optimized infinite loop in twenty
seconds.)

--
Eric Sosman
(E-Mail Removed)lid