Thread: What is faster and is there any difference?

  1. #1
    Registered User yann's Avatar
    Join Date
    Sep 2009
    Location
    Zagreb, Croatia
    Posts
    186

    What is faster and is there any difference?

    So, I'm working with an Arduino.

    I want to edit some of the core functions and i need to know - what is faster, a "for" or a "while" loop?

    Code:
    while(1)
    {
         do;
    };
    Code:
    for(;;)
    {
         do;
    }
    Which one of these will "do" faster? And maybe there is a faster way of looping things?
    Arduino rocks!

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    What? Is this a serious question?

  3. #3
    Registered User yann's Avatar
    Join Date
    Sep 2009
    Location
    Zagreb, Croatia
    Posts
    186
    ...Yeah?
    Arduino rocks!

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    If this is the first place you looked to optimize, have you scrutinized how an Arduino works, and tried to do any of that faster? All the looping code is is trivial for the computer to do.

  5. #5
    Banal internet user
    Join Date
    Aug 2002
    Posts
    1,380
    Put loops inside your loops to make them faster:
    Code:
    while (true)
    {
       while (true)
       {
          while (true)
          {
             // uber-fast!
          }
       }
    }

  6. #6
    Registered User yann's Avatar
    Join Date
    Sep 2009
    Location
    Zagreb, Croatia
    Posts
    186
    Lol that made me laugh... No really i had no idea, I guess I'm an idiot right now...
    Arduino rocks!

  7. #7
    Registered User yann's Avatar
    Join Date
    Sep 2009
    Location
    Zagreb, Croatia
    Posts
    186
    Well I thank god no one on Failblog is a programmer...

    I taught that the 'for' loop maybe needs to check if the iterator needs to be changed, and if you let the program run for like zillion years maybe it will be late for a few seconds, i don't know... Thats why i asked you guys.
    Arduino rocks!

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    "Premature optimization is the root of much evil."
    That is a great quote, and I suggested you heed it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    266
    My attempt at a serious answer.

    I would assume that for(; would be faster. My rational is that the "while(1) loop would need to be checked each time through for "1" each iteration through. Beyond that I doubt there would be any speed gain.

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    For a brandead compiler with all optimisations off, that might be the case. But for every other case it wont make the slightest bit of difference.
    for(;;) is slightly better though imho as it avoids a warning on certain compilers with certain warnings enabled.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Unless the body of the loop is comparable in duration to the empty loop (in other words, anything more complex than an empty if statement), then the time taken for the loop itself will be insignificant to the amount of work the loop is doing.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed