Raised This Month: $32 Target: $400
 8% 

Infinite loops


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Pro Patria Finland
Senior Member
Join Date: Apr 2006
Location: BaronPub.com
Old 02-12-2009 , 13:57   Infinite loops
Reply With Quote #1

An interesting question occured somewhere, I am not even sure if this question makes sense. Anywho, does anyone know which of these is the fastest one to execute, to make an infinite loop (in C++)?

Code:
while(1)
{

}
Code:
for(;;)
{

}
Code:
hello:

goto hello;
It doesn't matter what's inside the loop, just what of those is the fastest one to execute.
__________________
I am not a number. I am Gordon Freeman!
Pro Patria Finland is offline
Sean D
Junior Member
Join Date: Apr 2006
Old 02-12-2009 , 14:20   Re: Infinite loops
Reply With Quote #2

they all compile down into the same machine code
Sean D is offline
Jheshka
Senior Member
Join Date: Dec 2005
Old 02-12-2009 , 14:21   Re: Infinite loops
Reply With Quote #3

If there was one that's faster. I would think that for loops would be faster.




But what Sean D said sounds pretty accurate to me.
__________________
James

Last edited by Jheshka; 02-12-2009 at 14:29.
Jheshka is offline
Hawk552
AMX Mod X Moderator
Join Date: Aug 2005
Old 02-12-2009 , 14:29   Re: Infinite loops
Reply With Quote #4

Quote:
Originally Posted by Jheshka View Post
If there was one that's faster. I would think that for loops would be faster.




But what Sean D said sounds pretty accurate to me.
Hey man, the Agents have been looking for you. Hit me up on MSN later.

Also, the while loop is the fastest. The for loop as you put it won't work because there's no condition for continuation.
__________________
Hawk552 is offline
Send a message via AIM to Hawk552
Pro Patria Finland
Senior Member
Join Date: Apr 2006
Location: BaronPub.com
Old 02-12-2009 , 14:39   Re: Infinite loops
Reply With Quote #5

Quote:
Originally Posted by Hawk552 View Post
Hey man, the Agents have been looking for you. Hit me up on MSN later.

Also, the while loop is the fastest. The for loop as you put it won't work because there's no condition for continuation.
The for(;;) loop works fine. I even tested it again.

Code:
#include <iostream>

int main()
{
    for(;;)
        std::cout << "0";

    return 0;
}
Compiles and works.

Sean D: Based on what? Your knowledge as an experienced programmer? Could you provide some source? I am not asking to be an asshole. I am genuinely interested.

Edit: Reason why I am intersted is, that when programming for embedded systems the programs have to be as small as possible. I know this is a small matter, but I like to write as highly optimized code as possible for this project. In all of the examples I find in the micro-controllers datasheet a for(;;) loop is used.
__________________
I am not a number. I am Gordon Freeman!

Last edited by Pro Patria Finland; 02-12-2009 at 14:41.
Pro Patria Finland is offline
Hawk552
AMX Mod X Moderator
Join Date: Aug 2005
Old 02-12-2009 , 14:43   Re: Infinite loops
Reply With Quote #6

Interesting, I never knew that. Doesn't make sense, but oh well.
__________________
Hawk552 is offline
Send a message via AIM to Hawk552
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 02-12-2009 , 15:42   Re: Infinite loops
Reply With Quote #7

I agree with Hawk that the while() loop would be faster than the for() loop, but I can't compare with the goto because I'm not sure how fast it works.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
Hawk552
AMX Mod X Moderator
Join Date: Aug 2005
Old 02-12-2009 , 15:59   Re: Infinite loops
Reply With Quote #8

Quote:
Originally Posted by Exolent[jNr] View Post
I agree with Hawk that the while() loop would be faster than the for() loop, but I can't compare with the goto because I'm not sure how fast it works.
Goto is slower because it has to seek to a place that could possibly involve re-entry. I'm not totally sure about how it acts in C(++) but in TI-BASIC it's the worst thing you can do, several orders of magnitude slower than a for/while loop.

BTW, the reason I think the while loop would be faster is that it only takes 1 parameter. A for loop is basically a while loop that has 2 extra parameters (action after each iteration and action prior to first iteration), so there's no reason a for loop would be faster.
__________________
Hawk552 is offline
Send a message via AIM to Hawk552
danielkza
AMX Mod X Plugin Approver
Join Date: May 2007
Location: São Paulo - Brasil
Old 02-12-2009 , 16:38   Re: Infinite loops
Reply With Quote #9

Quote:
Originally Posted by Hawk552 View Post
Goto is slower because it has to seek to a place that could possibly involve re-entry. I'm not totally sure about how it acts in C(++) but in TI-BASIC it's the worst thing you can do, several orders of magnitude slower than a for/while loop.

BTW, the reason I think the while loop would be faster is that it only takes 1 parameter. A for loop is basically a while loop that has 2 extra parameters (action after each iteration and action prior to first iteration), so there's no reason a for loop would be faster.
As far as I know there's no such check in C++. Goto's translate directly to jump instructions in most cases.

And all the methods will generate the same machine code in any modern compiler.
__________________

Community / No support through PM
danielkza is offline
Sean D
Junior Member
Join Date: Apr 2006
Old 02-12-2009 , 16:59   Re: Infinite loops
Reply With Quote #10

experience? sure i guess... as danielkza was getting at, any modern compiler is going to know that the condition is always true so it will compile down to a jump. how much code it takes in the language doesn't determine how fast the machine code is; modern compilers are pretty smart. if you want proof then compile each and disassemble the executables in ida. by the way, if you're trying to optimize your code, you really should be using a profiler to pinpoint the bottleneck of your software so you don't have to waste time guessing.

also don't listen to anything hawk says, he doesn't know what he's talking about

Last edited by Sean D; 02-12-2009 at 17:05.
Sean D is offline
Reply



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 On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 18:37.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode