Which one is faster List<T> or ArrayList<T>?

16,803

Solution 1

I made this for you.

   static void Main(string[] args)
    {
        Stopwatch sw = new Stopwatch();
        Console.WriteLine("Adding a million 32bit integers");

        sw.Start();
        List<int> listA = new List<int>();
        for(int i = 0; i < 1000000; i++)
        {
            listA.Add(i);
        }
        sw.Stop();
        Console.WriteLine("List<int> took {0} ms", sw.ElapsedMilliseconds);
        sw.Reset();

        sw.Start();
        List<object> listB = new List<object>();
        for (int i = 0; i < 1000000; i++)
        {
            listB.Add(i);
        }
        sw.Stop();
        Console.WriteLine("List<object> took {0} ms", sw.ElapsedMilliseconds);
        sw.Reset();

        sw.Start();
        ArrayList listC = new ArrayList();
        for(int i = 0; i < 1000000; i++)
        {
            listC.Add(i);
        }
        sw.Stop();
        Console.WriteLine("ArrayList took {0} ms", sw.ElapsedMilliseconds);
        sw.Reset();

        Console.WriteLine("\n Inserting 1000 values");
        //Gen list of random numbers
        Random rand = new Random(12345);
        int[] insertlocs = new int[1000];
        for (int i = 0; i < insertlocs.Length; i++)
            insertlocs[i] = rand.Next(1, 999999);

        sw.Start();
        for (int i = 0; i < insertlocs.Length; i++)
        {
            listA.Insert(insertlocs[i], i);
        }
        sw.Stop();
        Console.WriteLine("List<int> took {0} ms", sw.ElapsedMilliseconds);
        sw.Reset();

        sw.Start();
        for (int i = 0; i < insertlocs.Length; i++)
        {
            listB.Insert(insertlocs[i], i);
        }
        sw.Stop();
        Console.WriteLine("List<object> took {0} ms", sw.ElapsedMilliseconds);
        sw.Reset();

        sw.Start();
        for (int i = 0; i < insertlocs.Length; i++)
        {
            listC.Insert(insertlocs[i], i);
        }
        sw.Stop();
        Console.WriteLine("ArrayList took {0} ms", sw.ElapsedMilliseconds);
        sw.Reset();

        Console.ReadKey();
    }

On my comp, List<int> took 13ms, List<object> took 69ms, ArrayList took 40ms.

So there you have it, for reference types ArrayList is faster. But for value types you should obviously use List

EDIT: Testing insert performance too, List<int> took 255ms, List<object> took 723ms, ArrayList took 397ms. ArrayList with boxing is almost on par with List without boxing!

Solution 2

I suppose you meant List<T> and ArrayList.

you should be using List<T> and not ArrayList because it doesn't have all the boxing\un-boxing.

Solution 3

I'm guessing it depends, are you talking about lookups, inserts, etc?

Share:
16,803
Everything Matters
Author by

Everything Matters

Updated on June 04, 2022

Comments

  • Everything Matters
    Everything Matters almost 2 years

    Possible Duplicate:
    .NET: ArrayList vs List

    Hello,

    I searched around the web and find my conflicting answers and so far unclear as to, Which one is faster List<T> or ArrayList<T> and what is the reason?

    I am guessing List<T> should be faster but not sure as in this specific case even ArrayList<T> also is marked as a generic type.

    Much Thanks, Mani

  • vittore
    vittore about 13 years
    +1 they both have their own pros and cons
  • Richard
    Richard about 13 years
    Try adding new Object() to ListB to avoid boxing (and note there is a typo: in 2nd test you're using ListA in the loop).
  • Hannesh
    Hannesh about 13 years
    I fixed the typo before posting my results but after copying the code, so it's still valid, but thanks for pointing it out. What do you mean? Values are boxed in both List<object> and ArrayList, casting the ints to objects doesn't change performance at all.
  • Darryl Braaten
    Darryl Braaten about 13 years
    One interesting thing is if you size the collections on creation List<T> gets faster and ArrayList gets slower, at least on my machine.
  • Hannesh
    Hannesh about 13 years
    Wow, significantly too! I'm going to take a peek at the classes to see what they do differently.
  • Richard
    Richard about 13 years
    @Hannesh: Indeed value types are boxed when added to both List<Object> and ArrayList, but then what are you comparing? One wouldn't use List<Object> except to be homogeneous. Two of the benefits of generic types are avoiding boxing and being typesafe.
  • Hannesh
    Hannesh about 13 years
    It's true that one wouldn't use List<object> but one would use List<SomeClass>. By testing List<object> we can properly compare the speed of ArrayList and List when adding refernce types since the boxing overhead is the same in both cases.