Compare performance of Array and ArrayList

Sourav.Kayal
Posted by in C# category on for Beginner level | Points: 250 | Views : 2747 red flag

Comparison of execution speed of Array and ArrayList

Compare performance of Array and ArrayList

In this quick tutorial we will compare execution speed of Array and ArrayList in C#. We know that ArrayList is one type of collection in C# and store object. In practical Array is more faster than ArrayList. In this article we will prove it practically.

Time taken to initialize Array and ArrayList

At first we will see the performance difference between Array and ArrayList in time to initialization.

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Collections;
namespace Client
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] Array = new int[1000];
            ArrayList li = new ArrayList(1000);
            Stopwatch sw = new Stopwatch();
           
            //Process with Array, Initialization
            sw.Start();
            for (int i = 0; i < 1000; i++)
            {
                Array[i] = i;
            }
            sw.Stop();
            Console.WriteLine("Array Takes :- " + sw.ElapsedTicks);
 
            //Process with ArrayList
            sw.Restart();
            for (int i = 0; i < 1000; i++)
            {
                li.Add(i);
            }
            sw.Stop();
            Console.WriteLine("ArrayList Takes :-" + sw.ElapsedTicks);
            Console.ReadLine();
        }
    }
}

Here is sample output.


 We are seeing that Array is nearly three times faster than ArrayLiat. Now, the question is why Array is faster than ArrayList in this scenario. The reason is Boxing and Unboxing.  When we are trying to initialize integer value to ArrayList it’s storing as object type rather than value type. This conversion taking time to initialize value in ArrayList. Whereas in case of Array this does not happen.  When we store value in array it get stored as value type.

Time taken to read Array and ArrayList

check with array.

In this example we will measure only accessing and printing time of data from Array. Here is sample example.

using System;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Collections;
namespace Client
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] Array = new int[1000];
            Stopwatch sw = new Stopwatch();
           
            //Process with Array, Initialization
            for (int i = 0; i < 1000; i++)
            {
                Array[i] = i;
            }
            sw.Start();
            for (int i = 0; i < 1000; i++)
            {
                Console.WriteLine(Array[i]);
            }
            sw.Stop();
            Console.WriteLine("Array Takes :- " + sw.ElapsedTicks);
            Console.ReadLine();
        }
    }
}

Output:-


Check with ArrayList:-

Now, we will measure time taken in time of ArrayList. Here is sample implementation.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Collections;
namespace Client
{
    class Program
    {
        static void Main(string[] args)
        {
            ArrayList li = new ArrayList(1000);
            Stopwatch sw = new Stopwatch();
           
            //Process with ArrayList, Initialization
           
            for (int i = 0; i < 1000; i++)
            {
                li.Add(i);
            }
            sw.Start();
 
            for (int i = 0; i < 1000; i++)
            {
                Console.WriteLine(li[i]);
            }
            sw.Stop();
            Console.WriteLine("ArrayList Takes :- " + sw.ElapsedTicks);
 
            Console.ReadLine();
        }
    }
}

Output:

Conclusion:-

We are seeing that in time of fetch operation Array and ArrayList is performing almost same.  So, In practical we have seen that in time of initialization Array performs well compare to ArrayList and in time of reading data both are very similar.

 

Page copy protected against web site content infringement by Copyscape

About the Author

Sourav.Kayal
Full Name: Sourav Kayal
Member Level: Silver
Member Status: Member,MVP
Member Since: 6/20/2013 2:09:01 AM
Country: India
Read my blog here http://ctrlcvprogrammer.blogspot.in/
http://www.dotnetfunda.com
I am .NET developer working for HelixDNA Technologies,Bangalore in healthcare domain. Like to learn new technology and programming language. Currently working in ASP.NET ,C# and other microsoft technologies.

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)