Translate

Sunday, January 13, 2013

C# Hashtable vs Dictionary vs Array vs ArrayList vs List search performance comparison

In this experiment I add these 5 people


Name = "Tom", Age = 32
Name = "John", Age = 22
Name = "Sandra", Age = 36
Name = "Julie", Age = 54
Name = "Samantha", Age = 21

into
1>A Hashtable,
2>A Dictionary<TKey, TValue>,
3>An Array,
4>An ArrayList and
5>A List<T>.

Then I try to find Samantha's age multiple times (Note that Samantha is the last entry) in each of these collections. These were the times taken.

Rank
Class
Time taken for the searches
1
Dictionary
 2.82 seconds
2
Hashtable
 2.96  seconds
3
Array
 4.68 seconds
4
List
 15.75 seconds
5
ArrayList
 30.08 seconds





Given below is the code used for the experiment


using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace DictionaryPerformanceComparison
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            const int searchCount = 99999999;
            DateTime begin;
            DateTime end;

            /*Create a hash table*/
            var hashTable = new Hashtable { { "Tom", 32 }, { "John", 22 }, { "Sandra", 36 }, { "Julie", 54 }, { "Samantha", 21 } };

            /*Create a dictionary*/
            var dict = new Dictionary<string, int>
                {
                    {"Tom", 32},
                    {"John", 22},
                    {"Sandra", 36},
                    {"Julie", 54},
                    {"Samantha", 21}
                };


            /*Create an array*/
            var pArray = new Person[5];
            pArray[0] = new Person { Name = "Tom", Age = 32 };
            pArray[1] = new Person { Name = "John", Age = 22 };
            pArray[2] = new Person { Name = "Sandra", Age = 36 };
            pArray[3] = new Person { Name = "Julie", Age = 54 };
            pArray[4] = new Person { Name = "Samantha", Age = 21 };


            /*Create an arraylist */
            var arLst = new ArrayList
                {
                    new Person {Name = "Tom", Age = 32},
                    new Person {Name = "John", Age = 22},
                    new Person {Name = "Sandra", Age = 36},
                    new Person {Name = "Julie", Age = 54},
                    new Person {Name = "Samantha", Age = 21}
                };


            /*Create a List*/
            var li = new List<Person>
                {
                    new Person {Name = "Tom", Age = 32},
                    new Person {Name = "John", Age = 22},
                    new Person {Name = "Sandra", Age = 36},
                    new Person {Name = "Julie", Age = 54},
                    new Person {Name = "Samantha", Age = 21}
                };


            begin = DateTime.Now;
            for (int i = 0; i < searchCount; i++)
            {
                int x = (int)hashTable["Samantha"];
            }
            end = DateTime.Now;
            Console.WriteLine("Hash table search took {0} seconds", end.Subtract(begin).TotalSeconds);


            begin = DateTime.Now;
            for (int i = 0; i < searchCount; i++)
            {
                int x = dict["Samantha"];
            }
            end = DateTime.Now;
            Console.WriteLine("Dictionary search took {0} seconds", end.Subtract(begin).TotalSeconds);


            begin = DateTime.Now;
            for (int i = 0; i < searchCount; i++)
            {
                int length = pArray.Length;
                for (int j = 0; j < length; j++)
                {
                    if (pArray[j].Name == "Samantha")
                    {
                        int age = pArray[j].Age;

                    }
                }
            }
            end = DateTime.Now;
            Console.WriteLine("Array search took {0} seconds", end.Subtract(begin).TotalSeconds);


            begin = DateTime.Now;
            for (int i = 0; i < searchCount; i++)
            {
                int x = arLst.OfType<Person>().FirstOrDefault(p => p.Name == "Samantha").Age;
            }
            end = DateTime.Now;
            Console.WriteLine("ArrayList search took {0} seconds", end.Subtract(begin).TotalSeconds);


            begin = DateTime.Now;
            for (int i = 0; i < searchCount; i++)
            {
                int x = li.FirstOrDefault(p => p.Name == "Samantha").Age;
            }
            end = DateTime.Now;
            Console.WriteLine("List search took {0} seconds", end.Subtract(begin).TotalSeconds);
        }
    }

    public class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }

}

7 comments:

  1. You constructed the array in wrong way to get its bad performance.

    ReplyDelete
    Replies
    1. I am not sure what you mean. Could you please elaborate?

      Delete
  2. Thanks, I had a problem similar to this and I was wondering about performance.

    In general;
    The comparison between a dictionary and hashtable is useful.
    The comparison between an Arraylist and List is also useful.

    Other than that, this is information helpful only if you are wanting to use a data structure where the lookup key is a string and you do not have many entries.

    When you are going through the array, you are looking at the first three entrees each time to get to Sandra. I suppose you chose that location to represent average lookup when your data set is only 5. If your data set was 1000, sequentially looking through 500 people objects would probably be significantly slower than using a hashing data structure such as a hashtable or dictionary (a hash map).

    It might be interesting to see how fast an array is if you used it as it was intended and looked up values rather than iterating through. You could write a simple hash function (get and set) that takes the first and last letters of a string and converts them to an int (char * char = index), then stores them in the proper location in the array. Then look up the items in the array using the function.

    It would also be interesting to see this same thing run with a int key lookup to a string value. That is where an array would shine.

    ReplyDelete
  3. this make sanse to me

    ReplyDelete

Comments will appear once they have been approved by the moderator