Which one is better in performance - a for loop or a while loop?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Shilpa Sethi
    New Member
    • Aug 2010
    • 12

    Which one is better in performance - a for loop or a while loop?

    I wanted to know which one is better in performance between a for loop and a while loop in terms of:
    1. memory requirement
    2. execution speed

    Also wanted to know since recursion functions are not as efficient as for of while in terms of memory requirement and execution speed, what is the purpose of using them in the code?
  • MartijnHoekstra
    New Member
    • Sep 2010
    • 39

    #2
    Hi with regards to while and for:
    - Memory requirement: The loops themselves cost no memory other than the jump statements, neglectable.
    - Execution speed: for simple expressions there will be no difference. But for advanced criteria to keep a loop running, a while loop could be more efficient. For example by use of switch cases or single if statements to break out of the loop.

    Recursions are in some cases necessary for special features, which otherwise would require extensive iterational coding.
    Best known example would be the solution to solve the 'towers of hanoi'.
    The efficiency will then be at development level. But at execution time, the efficiency loss will only be due to the call-stack.

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      I can't think of any reason for there to be a time- or space-efficiency difference between for and while loops.

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        If the loop body is executed N times, then for/while will evaluate the termination expression N+1 times but do while will only evaluate it N times.

        Comment

        • Oralloy
          Recognized Expert Contributor
          • Jun 2010
          • 988

          #5
          The main difference between the for/while and the do while is when the test is executed.

          Let's leave out the for loop's initializer and iteration; they will have to be coded when using while loops anyway.

          The only difference is that do while condition does not guard the first execution pass through the body of the loop. Said loop will always execute its body at least once.

          In the for and while loops, the condition is executed before the first pass through the body of the loop - meaning that the body of the loop is guarded against inadvertent execution.

          Set that aside, do while has fallen into general disfavor for a number of reasons, as the goto has. That is not to say that it does not have its place and value, just that it's in disfavor. Likely because when people write long (e.g. 100 line) loop bodies, it's very easy to lose track of the termination condition.

          Still, do while has its place, and belongs in your tool-kit.

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            I don't see that do/while has fallen out of favor.

            It is just a loop you use when you want the loop to execute at least once.

            Comment

            • Oralloy
              Recognized Expert Contributor
              • Jun 2010
              • 988

              #7
              weakness,

              I'm perhaps biased, but I've not seen it used in industrial code more than a dozen times in 25 years of experience.

              Now, I will admit that I've seen people go to ungodly lengths to simulate the do while, but actual use, almost never.

              Cheers!

              prrrrrr

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                I usuaally see this in menu processing where you want the user to provide a choice.

                If you use a while loop you need to get a choice outside the loop the first time and inside the loop the rest of the time. The do loop avoids the extra code.

                Comment

                • Oralloy
                  Recognized Expert Contributor
                  • Jun 2010
                  • 988

                  #9
                  Weakness,

                  I do understand.

                  Most of what I've worked with is machine control and GUI interfaces. Lots of interrupt driven code and state machines.

                  Thank you for the insight. I do appreciate it.

                  Cheers!

                  Comment

                  Working...