Wednesday, July 27, 2011

Dictionary and Hashtable Benchmarking

I made a simple test to see performance result between Dictionary and Hashtable in .NET framework by using C#. In this test I use :

.NET Framework 3.5 on Windows XP Professional OS with Service Pack 2. My PC hardware configuration is Intel(R) Pentium(R) Dual CPU E2160 1.80GHz and 4GB Memory.

In order to perform test I added 10 million items into Dictionary and Hashtable and retrieve all values from the collections in for loop. Each test was repeated 5 times.

Dictionary Collection Test Result :
Please look below for the results which are taking time to add values into Dictionary collection, to retrieve values from Dictionary collection and memory usage respectively.
add       get       memory usage
------------------------------
11.89sec | 4.22sec | 571,676KB
11.84sec | 4.38sec | 571,624KB
11.89sec | 4.22sec | 571,684KB
11.89sec | 4.22sec | 571,644KB
11.87sec | 4.22sec | 571,636KB
Hashtable Collection Test Result :
Please look below for the results which are taking time to add values into Hastable collectionto retrieve values from Hastable collection and memory usage respectively.
add       get       memory usage
------------------------------
17.27sec | 1,23sec | 630,692KB
17.24sec | 1.23sec | 630,692KB
17.18sec | 1.23sec | 630,632KB
17.24sec | 1.23sec | 630,672KB
17.49sec | 1.23sec | 630,672KB

Result:
Adding value into Dictionary collection is faster than Hashtable usage since retrieving value from Hashtable collection is faster than dictionary collection. But Hashtable needs more memory to store values than Dictionary collection.

I'm surprised because I assumed that retrieving object from Hashtable collection and convert it to string type takes time. But it's ~3.5 times faster than Dictionary collection.

Please don't forget some other tests such as adding different type values to Dictionary and Hashtable should be performed to get better test results to comprehend usage of two different kind of collections.

Source Code Used for Benchmarking
public class Program
{
        static Dictionary<int, string> dictionaryNumber = new Dictionary<int, string>();
        static Hashtable hash = new Hashtable();

        static void Main(string[] args)
        {
            TestDictionary();
            //TestHashtable();
            Console.Read();
        }

        static void TestHashtable()
        {
            long duration;
            long start;
            long end;
            long result;
            Console.WriteLine("adding");
            start = DateTime.Now.Ticks;
            // add 
            for (int i = 0; i < 10000000; i++)
            {
                hash.Add(i, i.ToString());
            }
            end = DateTime.Now.Ticks;

            duration = end - start;
            result = duration / 100000;
            Console.WriteLine(" result : " + result);

            Console.WriteLine("retrieve");
            // retrieve
            duration = 0;
            start = 0;
            end = 0;
            result = 0;

            start = DateTime.Now.Ticks;
            string s;
            for (int i = 0; i < 10000000; i++)
            {
                s = hash[i].ToString();
            }
            end = DateTime.Now.Ticks;

            duration = end - start;
            result = duration / 100000;

            Console.WriteLine(" result : " + result);
        }

        static void TestDictionary()
        {
            long duration;
            long start;
            long end;
            long result;
            Console.WriteLine("adding");
            start = DateTime.Now.Ticks;
            // add 
            for (int i = 0; i < 10000000; i++)
            {
                dictionaryNumber.Add(i, i.ToString());
            }
            end = DateTime.Now.Ticks;

            duration = end - start;
            result = duration / 10000;
            Console.WriteLine(" result : " + result);

            Console.WriteLine("retrieve");

            // retrieve
            duration = 0;
            start = 0;
            end = 0;
            result = 0;

            start = DateTime.Now.Ticks;
            string s;
            for (int i = 0; i < 10000000; i++)
            {
                s = dictionaryNumber[i];
            }
            end = DateTime.Now.Ticks;

            duration = end - start;
            result = duration / 10000;

            Console.WriteLine(" result : " + result);
        }
}

0 comments: