locked
Whats the difference between Arrays & ArrayList?

    Question

  • C#: Whats the difference between Arrays & ArrayList?
    Friday, June 18, 2010 5:54 PM

Answers

  • So, it seems that they are exactly same just Array is an abstract class and ArrayList isn't.

    Yasser,

     

    Array's and ArrayList are very different.  While the "class definition" is similar, the usage is quite different.  As Nishant said, arrays are useful if you have a fixed sized collection, and the size will never change.  In other cases, ArrayList (or even better, List<T> in .NET 2.0+) provides a collection that can be used in a method similar to an array, but is allowed to grow as needed.

     

    That being said, I recommend using List<T> instead of ArrayList.  ArrayList is now, for all purposes, obsolete - there is no reason to use it.  The difference in usage is like so (for an integer collection).

     

    int[] array = new int[5]; // Create with a specific size
    
    // Set the array to hold 5 elements, numbered 1-5
    for (int i=0;i<5;++i)
      array[i] = i+1;
    
    // Create a List<int> that holds the same elements 
    List<int> list = new List<int>();
    for (int i=0;i<5;++i)
      list.Add(i+1);
    
    // Access both in the same way:
    Console.WriteLine("Array: {0}, List: {1}", array[2], list[2]);
    
    // Change values the same way:
    array[3] = 23;
    list[3] = 23;
    
    // List can add values afterwards, though! Arrays cannot do this.
    list.Add(6);
    list.Add(7);
    
    


    Reed Copsey, Jr. - http://reedcopsey.com
    Friday, June 18, 2010 7:43 PM
  • In your example, the ArrayList is better. That's because the ArrayList takes less time to instantiate. Using the ArrayList however takes more time.

    On my machine, instantiating the List takes 5 times longer than instantiating an ArrayList. Adding an int to an ArrayList takes 4 times longer (boxing is responsible, but even adding objects to the ArrayList is twice slower than into a List<object>).

    The faster data manipulation compensates the slower instantiation around 700 items added.

    Saturday, June 19, 2010 5:58 PM
  • Basic difference is that arrays are of fixed size. Whereas an ArrayList implements the list data structure and can dynamically grow. While arrays would be more performant that a list, a list would be far more flexible since you don't need to know the required size initially.

    The .NET 1/1.1 ArrayList was type-unsafe but post-.NET 2.0, the framework comes with a generic list implementation. So that's a moot point now.

     


     

    http://blog.voidnish.com

    Friday, June 18, 2010 6:11 PM
  • Welcome to the MSDN Forums.

    //Array definition
    [SerializableAttribute]
    [ComVisibleAttribute(true)]
    public abstract class Array : ICloneable, 
    	IList, ICollection, IEnumerable
    
    //ArrayList definition
    [SerializableAttribute]
    [ComVisibleAttribute(true)]
    public class ArrayList : IList, ICollection, 
    	IEnumerable, ICloneable

    So, it seems that they are exactly same just Array is an abstract class and ArrayList isn't.

    Regards,

    Yasser


    LEARN HOW WPF IS FLEXIBLE IN PRESENTATION DURING A QUICK SIMPLE WALKTHROUGH:
    Walkthrough: Displaying multi column ComboBox by using Windows Presentation Foundation (WPF) data templating
    Friday, June 18, 2010 7:31 PM

All replies

  • Basic difference is that arrays are of fixed size. Whereas an ArrayList implements the list data structure and can dynamically grow. While arrays would be more performant that a list, a list would be far more flexible since you don't need to know the required size initially.

    The .NET 1/1.1 ArrayList was type-unsafe but post-.NET 2.0, the framework comes with a generic list implementation. So that's a moot point now.

     


     

    http://blog.voidnish.com

    Friday, June 18, 2010 6:11 PM
  • Welcome to the MSDN Forums.

    //Array definition
    [SerializableAttribute]
    [ComVisibleAttribute(true)]
    public abstract class Array : ICloneable, 
    	IList, ICollection, IEnumerable
    
    //ArrayList definition
    [SerializableAttribute]
    [ComVisibleAttribute(true)]
    public class ArrayList : IList, ICollection, 
    	IEnumerable, ICloneable

    So, it seems that they are exactly same just Array is an abstract class and ArrayList isn't.

    Regards,

    Yasser


    LEARN HOW WPF IS FLEXIBLE IN PRESENTATION DURING A QUICK SIMPLE WALKTHROUGH:
    Walkthrough: Displaying multi column ComboBox by using Windows Presentation Foundation (WPF) data templating
    Friday, June 18, 2010 7:31 PM
  • So, it seems that they are exactly same just Array is an abstract class and ArrayList isn't.

    Yasser,

     

    Array's and ArrayList are very different.  While the "class definition" is similar, the usage is quite different.  As Nishant said, arrays are useful if you have a fixed sized collection, and the size will never change.  In other cases, ArrayList (or even better, List<T> in .NET 2.0+) provides a collection that can be used in a method similar to an array, but is allowed to grow as needed.

     

    That being said, I recommend using List<T> instead of ArrayList.  ArrayList is now, for all purposes, obsolete - there is no reason to use it.  The difference in usage is like so (for an integer collection).

     

    int[] array = new int[5]; // Create with a specific size
    
    // Set the array to hold 5 elements, numbered 1-5
    for (int i=0;i<5;++i)
      array[i] = i+1;
    
    // Create a List<int> that holds the same elements 
    List<int> list = new List<int>();
    for (int i=0;i<5;++i)
      list.Add(i+1);
    
    // Access both in the same way:
    Console.WriteLine("Array: {0}, List: {1}", array[2], list[2]);
    
    // Change values the same way:
    array[3] = 23;
    list[3] = 23;
    
    // List can add values afterwards, though! Arrays cannot do this.
    list.Add(6);
    list.Add(7);
    
    


    Reed Copsey, Jr. - http://reedcopsey.com
    Friday, June 18, 2010 7:43 PM
  • Yasser,

    The difference in usage is like so (for an integer collection).

    Hi Reed, thanks for the prompt.

    I agree that they are suitable for different conditions, however, I meant, they are same in functionality. am I wrong? e.g. you can add values afterwards in arrays too using 'Array.Resize' method as below:

    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace ConsoleApplication1
    {
      class Program
      {
        static void Main(string[] args)
        {
          int[] array = new int[5]; // Create with a specific size
    
          // Set the array to hold 5 elements, numbered 1-5
          for (int i = 0; i < 5; ++i)
            array[i] = i + 1;
    
          // Change values the same way:
          array[3] = 23;
    
          // Create a List<int> that holds the same elements 
          List<int> list = new List<int>();
          for (int i = 0; i < 5; ++i)
            list.Add(i + 1);
    
          // Change values the same way:
          list[3] = 23;
    
          // List can add values afterwards
          list.Add(6);
          list.Add(7);
    
          //Arrays can do this too.
          Array.Resize<int>(ref array, array.Length + 2);
          array[array.Length - 2] = 6;
          array[array.Length - 1] = 7;
        }
      }
    }
    

     

    ArrayList is now, for all purposes, obsolete - there is no reason to use it.
    There is a reason named performance. Usually, more readable, reliable and modifiable means less performance. Generic Lists are more readable, reliable and modifiable than ArrayLists but they are losers in performance according to following sample code:
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Diagnostics;
    
    namespace ConsoleApplication1
    {
      class Program
      {
        static void Main(string[] args)
        {
          Stopwatch swGenericList = new Stopwatch();
          swGenericList.Start();
          List<int> genericList = new List<int>();
          for (int i = 0; i < 5; ++i)
            genericList.Add(i + 1);
          genericList[3] = 23;
          genericList.Add(6);
          genericList.Add(7);
          swGenericList.Stop();
    
          Stopwatch swArrayList = new Stopwatch();
          swArrayList.Start();
          ArrayList arrayList = new ArrayList();
          for (int i = 0; i < 5; ++i)
            arrayList.Add(i + 1);
          arrayList[3] = 23;
          arrayList.Add(6);
          arrayList.Add(7);
          swArrayList.Stop();
    
          Console.WriteLine("Generic List Elipsed Ticks: {0}\r\nArrayList Elipsed Ticks: {1}\r\nArrayList is {2} times faster than Generic List.", swGenericList.ElapsedTicks, swArrayList.ElapsedTicks, ((decimal)swGenericList.ElapsedTicks / swArrayList.ElapsedTicks).ToString("N2"));
          Console.ReadKey();
        }
      }
    }
    /*OUTPUT
    Generic List Elipsed Ticks: 116
    ArrayList Elipsed Ticks: 27
    ArrayList is 4.30 times faster than Generic List.
    */

    Best Wishes,

    Yasser


    LEARN HOW WPF IS FLEXIBLE IN PRESENTATION DURING A QUICK SIMPLE WALKTHROUGH:
    Walkthrough: Displaying multi column ComboBox by using Windows Presentation Foundation (WPF) data templating
    Saturday, June 19, 2010 5:52 AM
  • In your example, the ArrayList is better. That's because the ArrayList takes less time to instantiate. Using the ArrayList however takes more time.

    On my machine, instantiating the List takes 5 times longer than instantiating an ArrayList. Adding an int to an ArrayList takes 4 times longer (boxing is responsible, but even adding objects to the ArrayList is twice slower than into a List<object>).

    The faster data manipulation compensates the slower instantiation around 700 items added.

    Saturday, June 19, 2010 5:58 PM
  • Thank you Louis for the prompt and sorry Reed, you are right both.

    Actually, I forgot to verify two things:

    1. ArrayList methods against Array methods: I just compare their definitions.
    2. ArrayList performance against Generic List performance in large amount of data: I just measure them in small amount of data.

    And here is performance verifier in large amount of data:

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Diagnostics;
    
    namespace ConsoleApplication1
    {
      class Program
      {
        static void Main(string[] args)
        {
          Stopwatch swGenericList = new Stopwatch();
          swGenericList.Start();
          List<int> genericList = new List<int>();
          for (int i = 0; i < 7000; ++i)
            genericList.Add(i + 1);
          genericList[3] = 23;
          genericList.Add(6);
          genericList.Add(7);
          swGenericList.Stop();
    
          Stopwatch swArrayList = new Stopwatch();
          swArrayList.Start();
          ArrayList arrayList = new ArrayList();
          for (int i = 0; i < 7000; ++i)
            arrayList.Add(i + 1);
          arrayList[3] = 23;
          arrayList.Add(6);
          arrayList.Add(7);
          swArrayList.Stop();
    
          Console.WriteLine("Generic List Elipsed Ticks: {0}\r\nArrayList Elipsed Ticks: {1}\r\nArrayList is {2} times faster than Generic List.", swGenericList.ElapsedTicks, swArrayList.ElapsedTicks, ((decimal)swGenericList.ElapsedTicks / swArrayList.ElapsedTicks).ToString("N2"));
          Console.ReadKey();
        }
      }
    }
    /*OUTPUT
    Generic List Elipsed Ticks: 1389
    ArrayList Elipsed Ticks: 3458
    ArrayList is 0.40 times faster than Generic List.
     */

    Best Wishes,

    Yasser


    LEARN HOW WPF IS FLEXIBLE IN PRESENTATION DURING A QUICK SIMPLE WALKTHROUGH:
    Walkthrough: Displaying multi column ComboBox by using Windows Presentation Foundation (WPF) data templating
    Sunday, June 20, 2010 8:13 AM
  • Hi Reed, thanks for the prompt.

    I agree that they are suitable for different conditions, however, I meant, they are same in functionality. am I wrong? e.g. you can add values afterwards in arrays too using 'Array.Resize' method as below:

    Yasser,

    There is a big difference here.  When you call Array.Resize on an array, you're actually creating an entirely new array.  The new array, and the original array, will no longer have reference equality.  With an ArrayList (or better, a List<T>), when you add, as far as usage is concerned, you have the same actual object reference (since it's an internal private array that's regenerated).

     

    Try this:

     

    // Create the initial objects
    int[] array = new int[3] { 0,1,2};
    
    ArrayList list = new ArrayList();
    list.Add(0);
    list.Add(1);
    list.Add(2);
    
    // Save a copy (by reference)
    int[] arrayRef = array;
    ArrayList listRef = list;
    
    // Now add to them:
    
    Array.Resize<int>(ref array, array.Length+2);
    array[3] = 3;
    array[4] = 4;
    
    list.Add(3);
    list.Add(4);
    
    // The two contain the same elements, but...
    
    // Prints false - array is now a complete NEW array
    Console.WriteLine("array == arrayRef: {0}", array==arrayRef); // Prints true Console.WriteLine("list == listRef: {0}", list==listRef);

     

     


    Reed Copsey, Jr. - http://reedcopsey.com
    Monday, June 21, 2010 6:31 PM
  • Nice one Reed,

    It seems that Arrays are really arrays but ArrayLists and Generic Lists are linked lists, another difference which you discovered, a structurally difference. actually, you and Louis are right completely and your replies which I propose as answers, nicely describe 'The difference between Arrays and ArrayLists'.

    We can complete this difference list more, I think.

    Thank you.

    Regards,

    Yasser


    LEARN HOW WPF IS FLEXIBLE IN PRESENTATION DURING A QUICK SIMPLE WALKTHROUGH:
    Walkthrough: Displaying multi column ComboBox by using Windows Presentation Foundation (WPF) data templating
    Monday, June 21, 2010 7:40 PM
  • Nice one Reed,

    It seems that Arrays are really arrays but ArrayLists and Generic Lists are linked lists, another difference which you discovered, a structurally difference. actually, you and Louis are right completely and your replies which I propose as answers, nicely describe 'The difference between Arrays and ArrayLists'.

     

    Just for completeness:

     

    ArrayList and List<T> are actually a wrapper around an array.  Basically, they're a class that holds an array internally, and "resizes" it as needed (growing in chunks larger than the requested size, in order to reduce the number of reallocations required).  They're not actually linked lists (which is why they still have O(1) access time by index).

     

    Also, I'd strongly reconsider your stance on ArrayList being "faster" than List<T>.  Your benchmark above is somewhat skewed - you're staying in the smaller array sizes, which prevents any reallocation.  In addition, you're only benchmarking insertion into the ArrayList/List<T>, but not access time.  Try the following benchmark.  On my system, when run in release outside of VS, List<T> averages more than 2.5x faster than ArrayList, every time:

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Diagnostics;
    
    namespace ConsoleApplication1
    {
      class Program
      {
        static void Main(string[] args)
        {
          decimal ratio = 0m;
    
          const int runs = 1000;
    
          for (int run = 0; run < runs; ++run)
          {
            Stopwatch swGenericList = new Stopwatch();
    
            swGenericList.Start();
            List<int> genericList = new List<int>();
            genericList.Add(0);
            genericList.Add(1);
            for (int i = 0; i < 25; ++i)
            {
              genericList.Add(genericList[i] * genericList[i + 1]);
            }
            swGenericList.Stop();
    
            Stopwatch swArrayList = new Stopwatch();
            swArrayList.Start();
            ArrayList arrayList = new ArrayList();
            arrayList.Add(0);
            arrayList.Add(1);
            for (int i = 0; i < 25; ++i)
            {
              arrayList.Add((int)arrayList[i] * (int)arrayList[i + 1]);
            }
            arrayList.Add(6);
            arrayList.Add(7);
            swArrayList.Stop();
    
    
            ratio += (decimal)swArrayList.ElapsedTicks / (decimal)swGenericList.ElapsedTicks;
          }
    
          ratio /= runs;
    
          Console.WriteLine("({0} runs) List<T> is {1} times faster than ArrayList.", runs, ratio);
          Console.ReadKey();
        }
      }
    }

     


    Reed Copsey, Jr. - http://reedcopsey.com
    Monday, June 21, 2010 7:53 PM
  • Hi Reed,

    It seems that Generic List is 2010 World Cup winner. like Messi in Argentina, it's searching is very faster than Arrays and ArrayLists (almost 15x faster) according to this verifier code:

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Diagnostics;
    
    namespace ConsoleApplication1
    {
      class Program
      {
        static void Main(string[] args)
        {
          //Array
          byte[] array = new byte[1000000];
          Random rnd = new Random(DateTime.Now.Millisecond + DateTime.Now.Second * 1000);
          for (int i = 0; i < array.Length; i++)
            array[i] = (byte)rnd.Next(byte.MaxValue);
    
          //ArrayList
          ArrayList arrayList = new ArrayList(array);
    
          //List<int>
          List<byte> genericList = new List<byte>(array);
    
          //Search
          byte itemToSearch = (byte)rnd.Next(byte.MaxValue);
    
          //Array Search
          Stopwatch swArray = Stopwatch.StartNew();
          int arrayIndex = Array.IndexOf<byte>(array, itemToSearch);
          swArray.Stop();
    
          //ArrayList Search
          Stopwatch swArrayList = Stopwatch.StartNew();
          int arrayListIndex = arrayList.IndexOf(itemToSearch);
          swArrayList.Stop();
    
          //List<T> Search
          Stopwatch swGenericList = Stopwatch.StartNew();
          int genericListIndex = genericList.IndexOf(itemToSearch);
          swGenericList.Stop();
    
          //Result
          Console.WriteLine("Array: {0}", swArray.ElapsedTicks);
          Console.WriteLine("ArrayList: {0}", swArrayList.ElapsedTicks);
          Console.WriteLine("List<T>: {0}", swGenericList.ElapsedTicks);
    
          Console.ReadKey();
        }
      }
    }
    /*RELEASE OUTPUT
    Array: 672
    ArrayList: 644
    List<T>: 42
    */

    LEARN HOW WPF IS FLEXIBLE IN PRESENTATION DURING A QUICK SIMPLE WALKTHROUGH:
    Walkthrough: Displaying multi column ComboBox by using Windows Presentation Foundation (WPF) data templating
    Tuesday, June 22, 2010 12:18 PM
  • nice to see the all these comaprisons above; just curious to know the differences in List<Object> and ArrayList


    Manish Sati

    Tuesday, June 22, 2010 12:39 PM
  • Tried it with byte, int and object. Here are the mean times over 100 executions:

    byte[]: 10711
    ArrayList: 16398
    List<byte>: 1459

    int[]: 13278
    ArrayList: 19347
    List<int>: 2734

    object[]: 28970
    ArrayList: 14678
    List<object>: 8139

     

    Tuesday, June 22, 2010 1:26 PM
  • Array.IndexOf<> is very slow, and you should certainly avoid it for performance-critical stuff.

    The very fastest way of searching is to use a plain array and write a loop to search it yourself. Access to plain arrays is faster than access to List<> and much faster than access to ArrayList.

    Note that calling Array.IndexOf<> on a plain array IS NOT THE SAME AS accessing a plain array directly - it does lots of extra work.

     

    Tuesday, June 22, 2010 4:32 PM
  • You are right Matthew, thank you for the completion.

    "Manual loop serach" is Messi in Argentina now, and Array.IndexOf<T> is France without Zinadine Zidane.

    Here is the proof, however, I wonder why Stopwatch shows different results in each run?!!

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Diagnostics;
    
    namespace ConsoleApplication1
    {
      class Program
      {
        static void Main(string[] args)
        {
          //Array
          byte[] array = new byte[1000000];
          Random rnd = new Random(DateTime.Now.Millisecond + DateTime.Now.Second * 1000);
          for (int i = 0; i < array.Length; i++)
            array[i] = (byte)rnd.Next(byte.MaxValue);
    
          //ArrayList
          ArrayList arrayList = new ArrayList(array);
    
          //List<int>
          List<byte> genericList = new List<byte>(array);
    
          //Search
          byte itemToSearch = (byte)rnd.Next(byte.MaxValue);
    
          //Array Search
          Stopwatch swArray = Stopwatch.StartNew();
          //int arrayIndex = Array.IndexOf<byte>(array, itemToSearch);
          for (int arrayIndex = 0; arrayIndex < array.Length; arrayIndex++)
            if (array[arrayIndex] == itemToSearch)
              break;
          swArray.Stop();
    
          //ArrayList Search
          Stopwatch swArrayList = Stopwatch.StartNew();
          int arrayListIndex = arrayList.IndexOf(itemToSearch);
          swArrayList.Stop();
    
          //List<T> Search
          Stopwatch swGenericList = Stopwatch.StartNew();
          int genericListIndex = genericList.IndexOf(itemToSearch);
          swGenericList.Stop();
    
          //Result
          Console.WriteLine("Array: {0}", swArray.ElapsedTicks);
          Console.WriteLine("ArrayList: {0}", swArrayList.ElapsedTicks);
          Console.WriteLine("List<T>: {0}", swGenericList.ElapsedTicks);
    
          Console.ReadKey();
        }
      }
    }
    /*RELEASE OUTPUT
    Array: 28
    ArrayList: 434
    List<T>: 350
    */

    LEARN HOW WPF IS FLEXIBLE IN PRESENTATION DURING A QUICK SIMPLE WALKTHROUGH:
    Walkthrough: Displaying multi column ComboBox by using Windows Presentation Foundation (WPF) data templating
    Tuesday, June 22, 2010 5:01 PM
  •  I wonder why Stopwatch shows different results in each run?!!

    Maybe because you fill your array randomly.
    Tuesday, June 22, 2010 11:57 PM
  • Maybe because you fill your array randomly.

    Louis,

    It seems that Stopwatch is dependent to the Operating System condition. e.g.

    using System;
    using System.Diagnostics;
    using System.Threading;
    
    namespace ConsoleApplication1
    {
      class Program
      {
        static void Main(string[] args)
        {
          Stopwatch sw = Stopwatch.StartNew();
          Thread.Sleep(100);
          sw.Stop();
    
          Console.Write(sw.ElapsedTicks);
          Console.ReadKey();
        }
      }
    }
    /*DEBUG OUTPUT
    First Run : 361889 (First Compile)
    Second Run: 358324 (Without Compile)
    Third Run : 359964 (Without Compile)
    .
    .
    .
    */

    Actually, I don't know how Stopwatch works exactly and is this behaviour rational or not?


    LEARN HOW WPF IS FLEXIBLE IN PRESENTATION DURING A QUICK SIMPLE WALKTHROUGH:
    Walkthrough: Displaying multi column ComboBox by using Windows Presentation Foundation (WPF) data templating
    Wednesday, June 23, 2010 8:06 AM
  • No, Stopwatch is extremely accurate (it uses the processor's High Resolution counters).

    What ISN'T accurate is Thread.Sleep(), which is normally only accurate to within 20-50ms.

    Wednesday, June 23, 2010 8:38 AM
  • No, Stopwatch is extremely accurate (it uses the processor's High Resolution counters).

    What ISN'T accurate is Thread.Sleep(), which is normally only accurate to within 20-50ms.


    Matthew,

    There are different results for 'for-loop' too as below:

    using System;
    using System.Diagnostics;
    using System.Threading;
    
    namespace ConsoleApplication1
    {
      class Program
      {
        static void Main(string[] args)
        {
          Stopwatch sw = Stopwatch.StartNew();
          for (int i = 0; i < 1000000; i++)
            ;
          sw.Stop();
    
          Console.Write(sw.ElapsedTicks);
          Console.ReadKey();
        }
      }
    }
    /*DEBUG OUTPUT
    First Run:  34141 (First Compile)
    Second Run: 110716 (Without Compile)
    Third Run:  34151 (Without Compile)
    Fourth Run: 36731 (Without Compile)
    */

    However, maybe it's my system problem! may I see your result please?

    Thank you.


    LEARN HOW WPF IS FLEXIBLE IN PRESENTATION DURING A QUICK SIMPLE WALKTHROUGH:
    Walkthrough: Displaying multi column ComboBox by using Windows Presentation Foundation (WPF) data templating
    Wednesday, June 23, 2010 8:50 AM
  • That will be how long it actually took to execute. Stopwatch is very accurate; but on a multitasking operating system when you are running code that is being jitted, you will get timing differences.

    The differences in time you're seeing between three of those runs in in the order of 1 or 2 milliseconds!

    The second run was clearly affected by something - probably something the operating system was up to.

    Test programs on a non-real time operating system that attempt top measure differences in time of less than 20 milliseconds or so are usually fairly meaningless. I normally aim to time things that last at least a second (by calling the test code in a tight loop).

    These are the results I get:

    Debug build:

    7507
    7948
    7647
    8058

    Release build:

    245
    244
    245
    245

    Stopwatch.Frequency on my system is 2078164, so you must divide those values by 2078164 to get seconds. When you do so, you can see the differences are very small.

    Since Stopwatch.Frequency can vary between computers, reporting timings as elapsed ticks is not very useful. It's better to use Stopwatch.Elapsed.

    Stopwatch uses the Windows API QueryPerformanceCounter() and QueryPerformanceFrequency() under the hood - check those functions out for complete details on its accuracy. I must reiterate: There is no more accurate way to measure time intervals on an Intel or AMD processor.
    Wednesday, June 23, 2010 9:42 AM
  • Matthew brought up many very valid points.

     

    I'll also add: When doing timings, you should always time something, especially if it's a fast loop like this, by running it multiple times, and preferably averaging out or summing the total runtimes.  The JIT overhead will be larger than the runtime the first time through this program, most likely, so any timings will be highly suspect on the first run.

     

    This is why, in my code above, I actually run this check 1000 times, and average the total over 1000 runs.  This gives you a much better feel of what's truly faster in real applications.

     

    In addition, make sure to never run your timings inside of Visual Studio (or, if you do, use Ctrl+F5, not F5, to start).  The VS test host will dramatically impact your timings, which will most likely cause wild fluctuations in your runtimes.

     


    Reed Copsey, Jr. - http://reedcopsey.com
    Wednesday, June 23, 2010 3:54 PM
  • // Prints true
    Console.WriteLine("array == arrayRef: {0}", array==arrayRef);
    
    // Prints false - array is now a complete NEW array
    Console.WriteLine("list == listRef: {0}", list==listRef);
    Hi Reed,

    Thanks for the nice explanation of Array and ArrayList as I was finding the defference between Array and ArrayList.
    Can you please edit your above code as It written wrong. first condition is return FALSE and second return is TRUE.:)

    Thanks,
    Mohit 
    Friday, November 19, 2010 9:01 AM
  • Mohit:

     

    Thanks for pointing out that comment issue - it's corrected now.

     

    -Reed

     


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Friday, November 19, 2010 4:37 PM
  • hi Yasser,

    I'm fresher.so plz define a easy way array,collection & generics means that interviewer couldn't ask me cross questions and if he ask so i should have a good example  So help me:

     

    Bhushan

     

    Tuesday, January 24, 2012 11:31 AM