For loop and while loop. which one is faster?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mgcclx@gmail.com

    For loop and while loop. which one is faster?

    For loop and while loop. which one is faster?
    I see many articles fighting over it and different people come up with
    different results.

  • =?ISO-8859-15?Q?Iv=E1n_S=E1nchez_Ortega?=

    #2
    Re: For loop and while loop. which one is faster?

    mgcclx@gmail.co m wrote:
    For loop and while loop. which one is faster?
    I see many articles fighting over it and different people come up with
    different results.
    Unless you're designing a compiler, don't worry about that. Worry about the
    algorithms instead.

    That said, the only possible difference between a for loop and a while loop
    is the jump prediction in your architecture's code instruction set. e.g.
    the processor can suppose that the condition for a while loop is going to
    be true, so it feeds the instructions after the conditional jump (that is,
    it's supposing that the jump condition will be successful) and starts
    running them in the first stages of the instruction pipeline, saving a
    couple of pipeline stages.

    Do you have to worry about processor pipelines and jump prediction? Not at
    all, unless you're into assembler, or heavy low-level programming. If you
    haven't understood a word of the last paragraph, don't worry about loop
    speed.


    In other words: trust your compiler (or JIT compiler, or interpreter). It
    knows about optimizations of conditional jumps better than you (the same
    applies to memory caching). Focus on the algorithmics if you want to
    shorten processor time.

    --
    ----------------------------------
    Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

    Give a man a fire and he'll be warm for a day; set him on fire and he'll be
    warm the rest of his life.

    Comment

    • Tim Streater

      #3
      Re: For loop and while loop. which one is faster?

      In article <eu9m2t$af5$1@h ercules.cohp1>,
      Iván Sánchez Ortega
      <ivansanchez-alg@rroba-escomposlinux.-.punto.-.orgwrote:
      mgcclx@gmail.co m wrote:
      >
      For loop and while loop. which one is faster?
      I see many articles fighting over it and different people come up with
      different results.
      >
      Unless you're designing a compiler, don't worry about that. Worry about the
      algorithms instead.
      >
      That said, the only possible difference between a for loop and a while loop
      is the jump prediction in your architecture's code instruction set. e.g.
      the processor can suppose that the condition for a while loop is going to
      be true, so it feeds the instructions after the conditional jump (that is,
      it's supposing that the jump condition will be successful) and starts
      running them in the first stages of the instruction pipeline, saving a
      couple of pipeline stages.
      >
      Do you have to worry about processor pipelines and jump prediction? Not at
      all, unless you're into assembler, or heavy low-level programming. If you
      haven't understood a word of the last paragraph, don't worry about loop
      speed.
      >
      In other words: trust your compiler (or JIT compiler, or interpreter). It
      knows about optimizations of conditional jumps better than you (the same
      applies to memory caching). Focus on the algorithmics if you want to
      shorten processor time.
      This is quite right. I never worry about such things - that's the
      computer's job.

      I use whichever is appropriate - try, in other words, to give a hint to
      yourself or whoever is going to look at that code in a year's time. If I
      have n items and I want to do something for each of them, I use a for
      loop. If I need to loop around until some condition is satisfied (maybe
      one of several conditions in different parts of the loop), then I use a
      while.

      -- tim

      Comment

      • Colin McKinnon

        #4
        Re: For loop and while loop. which one is faster?

        mgcclx@gmail.co m wrote:
        For loop and while loop. which one is faster?
        I see many articles fighting over it and different people come up with
        different results.
        Why don't you test them if its that important?

        C.

        Comment

        • Thomas Mlynarczyk

          #5
          Re: For loop and while loop. which one is faster?

          Also sprach mgcclx@gmail.co m:
          For loop and while loop. which one is faster?
          I don't think it makes much of a difference internally.

          for ( $i = 0; $i < 10; $i++ )
          {
          // do stuff
          }

          is probably treated just like the - less elegant -

          $i = 0;
          while ( $i < 10 )
          {
          // do stuff
          $i++;
          }

          with no performance gain or loss. On the other hand, it makes a huge
          difference in performance if you write

          for ( $i = 0; $i < count( $a ); $i++ ) { /* do stuff */ }

          or

          for ( $i = count( $a ); $i--; ) { /* do stuff */ }

          the latter being much faster (function calls - count() in this example -
          cost time and the second version is one statement shorter). Of course, it
          iterates backwards over $a, but there are many cases where this does not
          matter. So, if you want to optimize your code, reduce the number of function
          calls, reduce the number of statements and reduce the number of variables
          (especially for intermediate results).

          Greetings,
          Thomas


          Comment

          • Malcolm Dew-Jones

            #6
            Re: For loop and while loop. which one is faster?

            mgcclx@gmail.co m wrote:
            : For loop and while loop. which one is faster?
            : I see many articles fighting over it and different people come up with
            : different results.

            What ever you find, it will likely be wrong next year, And different again
            the year after that.

            Use the construct that makes the most sense.

            Comment

            • jodleren

              #7
              Re: For loop and while loop. which one is faster?

              On Mar 27, 1:45 am, mgc...@gmail.co m wrote:
              For loop and while loop. which one is faster?
              I see many articles fighting over it and different people come up with
              different results.
              a bit late, but...
              AFAIK it is faster to count down than up. Dont know about while or
              for, but they are pretty much the same.

              Personally I use:
              $i=888834675684 57642645;
              while(0<$i--)
              blabla;


              Comment

              Working...