Loops performance contradictions.

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Sean Wolfe (swolfeAtHubbardOneDotCom_nospam_at_nospam.com)
Date: 11/12/04

  • Next message: Lloyd Dupont: "Re: Loops performance contradictions."
    Date: Thu, 11 Nov 2004 18:14:10 -0600
    
    

    I've read many items about looping and performance issue on how you use
    loops. I was bringing this up with other co-workers and there was a
    slight dispute. I made a test example, and in most cases, it proved what
    I have come to understand, and in some it surprised me.

    As far as I understand it in most cases you want to save your comparator
      outside of the for loop, so that the calculation or operation doesn't
    happen on each iteration (unless it will change during the loop).

    Example:

    //the slow way
    for(int index = 0; index < collection.Count; index++) {
            // Do something
    }

    // the fast way
    int colCount = collection.Count;
    for(int index = 0; index < colCount; index++) {
            // do something
    }

    But there is the exception with arrays, which seems to have a special
    case, that the JIT/ compiler is able to optimize for array's length property

    Example:
    for(int index = 0; index < myArray.Length; index++) {
            // do something
    }

    // is the same as
    int arrayLength = myArray.Length;
    for(int index = 0; index < arrayLength; index++) {
            // do something
    }

    I have also read that some say that the latter is sometimes slower
    because of the optimizations.

    Another issue, is that foreach loops are slower for certain collections,
    especially if the collection is homogeneous than for(;;) loops.

    But let me list my example code where I had some issues. A bit of
    waring, some people may want to adjust the size of the Arrays because in
    my example they allocate a large amount of RAM (especially if you don't
    have a lot of memory on your machine).

    using System;

    public class LoopTest
    {
         public static void Main()
         {
             Console.WriteLine("Creating an array of length 0x7735940.");
             int[] data = new int[0x7735940];

             Console.WriteLine("Filling array with values.");
             long startTicks = DateTime.Now.Ticks;
             for(int index = 0; index < 0x7735940; index++)
             {
                 data[index] = index;
             }
             long endTicks = DateTime.Now.Ticks;

             long tickDiff = endTicks - startTicks;
             Console.WriteLine("Filled array in about {0} ticks.", tickDiff);

             Console.WriteLine("Using iteration by storing value first.");
             startTicks = DateTime.Now.Ticks;
             int loopLen = data.Length;
             for(int index = 0; index < loopLen; index++)
             {
                 data[index] = loopLen - index;
             }
             endTicks = DateTime.Now.Ticks;
             tickDiff = endTicks - startTicks;

             Console.WriteLine("Set the array data in about {0} ticks.",
    tickDiff);

             Console.WriteLine("Using iteration using built in Array
    optimization.");
             startTicks = DateTime.Now.Ticks;
             for(int index = 0; index < data.Length; index++)
             {
                 data[index] = index + loopLen;
             }
             endTicks = DateTime.Now.Ticks;
             tickDiff = endTicks - startTicks;

             Console.WriteLine("set the array data in about {0} ticks using
    built in iteration.", tickDiff);
         }

    }

    Here is the results that I got from one of the runs.

    Creating an array of length 0x3B9ACA0.
    Filling array with values.
    Filled array in about 4532410 ticks.
    Using iteration by storing value first.
    Set the array data in about 3438380 ticks.
    Using iteration using built in Array optimization.
    set the array data in about 3125800 ticks using built in iteration.
    Using iteration with foreach.
    set the array data in about 4376120 ticks using enumeration.
    Creating an ArrayList of length 0x1DCD650.
    Filling List with values.
    Filled array in about 95649480 ticks.
    Using iteration by storing Count first.
    Set the List data in about 256628180 ticks.
    Using iteration without storing Count first.
    Set the List data in about 245687880 ticks.
    Using iteration with foreach.
    set the array data in about 11721750 ticks using enumeration.

    In all cases the results showed that an array looped by it's length in
    the comparison clause or stored in a variable beforehand had no
    difference. The thing that was odd to me, is that the foreach loop was
    much faster by a magnitude than the for loop. Is there some kind of
    optimization in my statement going on here that I don't know about?

    Any suggestions?

    Sean


  • Next message: Lloyd Dupont: "Re: Loops performance contradictions."

    Relevant Pages

    • Re: Loops performance contradictions.
      ... call are inlind when JITed, it's what happened to your array, aray.Count is ... > happen on each iteration. ... > Set the array data in about 3438380 ticks. ... > set the array data in about 3125800 ticks using built in iteration. ...
      (microsoft.public.dotnet.framework.performance)
    • Re: Loops performance contradictions.
      ... how do you set the data using foreach??? ... You're just writing to the array in a tight loop. ... >> happen on each iteration. ... >> Set the array data in about 3438380 ticks. ...
      (microsoft.public.dotnet.framework.performance)
    • Re: How to create descendants of Array class?
      ... show up in a for-in loop and similar operations, ... The augmented array facilities direct indexed looping and maintains its ... aggregated through a reference to it by the aggregating ... object's property) would allow clean for-in iteration of the ...
      (comp.lang.javascript)
    • Re: Visualbasic memory crash
      ... The main program is almost impossible to troubleshoot, but here are a few things I noticed: ... - The loop in the upper right has no delay. ... one iteration, followed by built array and transpose array. ...
      (comp.lang.labview)
    • Re: How to dynamically resize an array?
      ... The man said that he will resize the array once! ... even if he does want to resize it more than once he very definitely never said that he was goiung to resize it at every iteration of the loop. ...
      (microsoft.public.dotnet.languages.vb)