Re: Loops performance contradictions.
From: Lloyd Dupont (ld_at_NewsAccount.galador.net)
Date: 11/12/04
- Next message: Stu Smith: "Re: Question on Garbage Collection"
- Previous message: Sean Wolfe: "Loops performance contradictions."
- In reply to: Sean Wolfe: "Loops performance contradictions."
- Next in thread: Stefan Simek: "Re: Loops performance contradictions."
- Reply: Stefan Simek: "Re: Loops performance contradictions."
- Messages sorted by: [ date ] [ thread ]
Date: Fri, 12 Nov 2004 16:21:47 +1100
there are multiple performance trick happening
1. inlining:
it's due to automatic inlining.
if a method call is less than a given treshold (32 byte of bytecode) method
call are inlind when JITed, it's what happened to your array, aray.Count is
inlined
2. bounds check eliminiation
i vary nicely from 0 to lenght, the test is done only once
and maybe others...
it's not because it's an array list, it's just because the JIT could figure
out this optimization easily in your cases
"Sean Wolfe" <swolfeAtHubbardOneDotCom_nospam@nospam.com> wrote in message
news:evtxJyEyEHA.2036@TK2MSFTNGP12.phx.gbl...
> 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: Stu Smith: "Re: Question on Garbage Collection"
- Previous message: Sean Wolfe: "Loops performance contradictions."
- In reply to: Sean Wolfe: "Loops performance contradictions."
- Next in thread: Stefan Simek: "Re: Loops performance contradictions."
- Reply: Stefan Simek: "Re: Loops performance contradictions."
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|