none
Which 'FOR' loop has better performance? Increment or decrement ? RRS feed

  • Question

  • Hi,
    which loop has better performance? I have learnt from some where that second is better. But want to know reason why.

    A)   for(int i=0;i<10;i++)
          {
                   //Dop Something
          }
    B)   for(int i=10;i>0;i--)
          {
                   //Dop Something
          }
    Wednesday, May 20, 2009 9:17 AM

Answers

  • The two should be identical. You can measure this yourself using WPT - just make it bigger than 10. You'll need to log ETW events before/after the loop to precisely measure how long it took to do the loop.

    Cheers,
    Michael
    Tuesday, May 26, 2009 6:43 PM

All replies

  • The two should be identical. You can measure this yourself using WPT - just make it bigger than 10. You'll need to log ETW events before/after the loop to precisely measure how long it took to do the loop.

    Cheers,
    Michael
    Tuesday, May 26, 2009 6:43 PM

  • I thought "performence counter" should be used.
    Is it possible to use perf counter to measure.
    Thanks
    Charles


    C--s
    Wednesday, August 5, 2009 7:57 AM
  • I'm using Manifest Generator Tool Reference Guide to generate manifest for measuring function entry and exit point where it defines event payload and talks about FunctionName, 'FunctionName' here refering is actual function name in the Sample.exe? 


    C--s
    Wednesday, August 5, 2009 8:21 AM
  • Hi,
    which loop has better performance? I have learnt from some where that second is better. But want to know reason why.

    A)   for(int i=0;i<10;i++)
          {
                   //Dop Something
          }
    B)   for(int i=10;i>0;i--)
          {
                   //Dop Something
          }

    very interesting question.
    Monday, September 14, 2009 2:44 PM

  •  Got the answer? Can you share it with us? Thank you!
    Thursday, November 25, 2010 5:14 PM
  • Friday, March 11, 2011 8:53 PM