Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old March 26th, 2006, 04:55 AM
Vector
Guest
 
Posts: n/a
Default infinite loops

Is any infinite loop better than other? Is there any difference between
there efficiency?

  #2  
Old March 26th, 2006, 05:45 AM
Phlip
Guest
 
Posts: n/a
Default Re: infinite loops

Vector wrote:
[color=blue]
> Is any infinite loop better than other? Is there any difference between
> there efficiency?[/color]

To what use will you put the answer? A compiler will treat for(;;) and
while(true) the same. The second takes longer to type. Always optimize
programming time.

The second might trigger a "conditional expression is constant" warning of
some type.

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!


  #3  
Old March 26th, 2006, 05:55 AM
Ian Collins
Guest
 
Posts: n/a
Default Re: infinite loops

Vector wrote:[color=blue]
> Is any infinite loop better than other? Is there any difference between
> there efficiency?
>[/color]
Define better.

Which conveys the intent better? I'd say while( true ){}

Efficiency shouldn't be an issue.

--
Ian Collins.
  #4  
Old March 26th, 2006, 06:45 AM
red floyd
Guest
 
Posts: n/a
Default Re: infinite loops

Vector wrote:[color=blue]
> Is any infinite loop better than other? Is there any difference between
> there efficiency?
>[/color]

I'm going to assume this is a joke.

Have you determined that the infinite loop is your bottleneck? How
long does your infinite loop take to complete? Have you benchmarked to
see if your infinite loop runs in aleph-null time versus aleph-one time?



  #5  
Old March 26th, 2006, 10:45 AM
Tomás
Guest
 
Posts: n/a
Default Re: infinite loops

Vector posted:
[color=blue]
> Is any infinite loop better than other? Is there any difference between
> there efficiency?[/color]


In practise, they are the same. Compilers will produce the exact same
machine code for these:

for (;;) { int k = 7; }

while (true) { int k = 7; }

do { int k = 7; } while (true);


In theory, the first one is fastest because there's no condition being
tested for each iteration of the loop.

I myself use for(;;).


-Tomás
  #6  
Old March 26th, 2006, 12:35 PM
Gaijinco
Guest
 
Posts: n/a
Default Re: infinite loops

This is slightly off-topic but I found it intresting: What does this
code prints?

#include <iostream>

int main()
{
int i=1;

while(i>0)
++i;

std::cout << i;

return 0;
}

  #7  
Old March 26th, 2006, 01:55 PM
amt.dwivedi@gmail.com
Guest
 
Posts: n/a
Default Re: infinite loops


Gaijinco wrote:[color=blue]
> This is slightly off-topic but I found it intresting: What does this
> code prints?
>
> #include <iostream>
>
> int main()
> {
> int i=1;
>
> while(i>0)
> ++i;
>
> std::cout << i;
>
> return 0;
> }[/color]
well answer is very obvious
it will print negative number that is shortest one.
reason : consider the same code with little modification for ur
understanding
int main()
{
char i=1;

while(i>0)
++i;

std::cout << int(i);
std::cin.get();

return 0;
}
print -128
i increments from 1 to 127 than incrementing further will make it 127
to -128
this is wat the range of char -128 to 127.

  #8  
Old March 26th, 2006, 01:55 PM
Tomás
Guest
 
Posts: n/a
Default Re: infinite loops

Gaijinco posted:
[color=blue]
> This is slightly off-topic but I found it intresting: What does this
> code prints?
>
> #include <iostream>
>
> int main()
> {
> int i=1;
>
> while(i>0)
> ++i;
>
> std::cout << i;
>
> return 0;
> }[/color]


As far as I know it's undefined behaviour for a signed integral type to go
past its limit.

In practise though, on a 32-Bit system, I'd hazard a guess that it'd be one
of the following:

0
-1
-4294967296


-Tomás
  #9  
Old March 26th, 2006, 03:35 PM
Rolf Magnus
Guest
 
Posts: n/a
Default Re: infinite loops

amt.dwivedi@gmail.com wrote:
[color=blue]
>
> Gaijinco wrote:[color=green]
>> This is slightly off-topic but I found it intresting: What does this
>> code prints?
>>
>> #include <iostream>
>>
>> int main()
>> {
>> int i=1;
>>
>> while(i>0)
>> ++i;
>>
>> std::cout << i;
>>
>> return 0;
>> }[/color]
> well answer is very obvious
> it will print negative number that is shortest one.
> reason : consider the same code with little modification for ur
> understanding
> int main()
> {
> char i=1;
>
> while(i>0)
> ++i;
>
> std::cout << int(i);
> std::cin.get();
>
> return 0;
> }
> print -128
> i increments from 1 to 127 than incrementing further will make it 127
> to -128[/color]

It might do this, or it might do anything else. The overflow behavior of
signed integers is undefined.


  #10  
Old March 27th, 2006, 04:25 AM
Jack Klein
Guest
 
Posts: n/a
Default Re: infinite loops

On 25 Mar 2006 20:42:34 -0800, "Vector" <sunnypalsingh@gmail.com>
wrote in comp.lang.c++:
[color=blue]
> Is any infinite loop better than other? Is there any difference between
> there efficiency?[/color]

How could one infinite loop be better than another? Could one be
infinitely more infinite than the other? If one ends before the other
does, the comparison was invalid because the one that ends was not an
infinite loop to begin with.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
  #11  
Old March 27th, 2006, 05:45 AM
Phlip
Guest
 
Posts: n/a
Default Re: infinite loops

Jack Klein wrote:
[color=blue]
> How could one infinite loop be better than another? Could one be
> infinitely more infinite than the other? If one ends before the other
> does, the comparison was invalid because the one that ends was not an
> infinite loop to begin with.[/color]

Haw haw.

That sophistry is either a play on James Kanze's latest post to the thread
"Infinite loop == undefined behaviour?"...

http://groups.google.com/group/comp....69f9d5e793835b

....or a supernatural channeling of it, if you hadn't read it yet.

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!


  #12  
Old March 27th, 2006, 06:45 AM
Old Wolf
Guest
 
Posts: n/a
Default Re: infinite loops

Rolf Magnus wrote:[color=blue]
> amt.dwivedi@gmail.com wrote:[color=green]
>>
>> char i=1;
>>
>> while(i>0)
>> ++i;
>>
>> std::cout << int(i);
>> std::cin.get();
>>
>> return 0;
>> }
>> print -128[/color]
>
> It might do this, or it might do anything else. The overflow behavior of
> signed integers is undefined.[/color]

This is actually a case of out-of-range assignment: i is promoted
to int for the expression ++i, and the result is then reassigned
to i.

  #13  
Old March 27th, 2006, 01:15 PM
Rolf Magnus
Guest
 
Posts: n/a
Default Re: infinite loops

Old Wolf wrote:
[color=blue]
> Rolf Magnus wrote:[color=green]
>> amt.dwivedi@gmail.com wrote:[color=darkred]
>>>
>>> char i=1;
>>>
>>> while(i>0)
>>> ++i;
>>>
>>> std::cout << int(i);
>>> std::cin.get();
>>>
>>> return 0;
>>> }
>>> print -128[/color]
>>
>> It might do this, or it might do anything else. The overflow behavior of
>> signed integers is undefined.[/color]
>
> This is actually a case of out-of-range assignment:[/color]

<nitpick>unless char is signed and sizeof(int)==sizeof(char)</nitpick>.
[color=blue]
> i is promoted to int for the expression ++i, and the result is then
> reassigned to i.[/color]

The other example that used int for i was doing what I wrote. Anyway, what
would be the behavior here? Is the value just truncated (modulo) or is it
UB too?

  #14  
Old March 27th, 2006, 09:45 PM
Old Wolf
Guest
 
Posts: n/a
Default Re: infinite loops

Rolf Magnus wrote:[color=blue]
> Old Wolf wrote:
>[color=green][color=darkred]
>>> amt.dwivedi@gmail.com wrote:
>>>>
>>>> char i=1;
>>>>
>>>> while(i>0)
>>>> ++i;[/color]
>> This is actually a case of out-of-range assignment:[/color]
>
> <nitpick>unless char is signed and sizeof(int)==sizeof(char)</nitpick>.[/color]

Of course :)
[color=blue]
> The other example that used int for i was doing what I wrote. Anyway, what
> would be the behavior here? Is the value just truncated (modulo) or is it
> UB too?[/color]

Being signed, I think it is a case of implementation-defined
behaviour or an i-d signal in C99. (

 

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles