Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / programming / performance

Performance measurement of HashTable vs. Dictionary

4.86/5 (10 votes)
24 Sep 2013CPOL1 min read 68.9K  
Case study for checking the performance of HashTable and Dictionary.

Introduction

Here we are going to discuss about one of the cases for performance between HashTable and Dictionary. I have taken some sample data to check for performance between HashTable and Dictionary. We have a test case to check for multiple data given in an XML datasource. You can add many data for testing. We are testing for writing data into Dictionary and HashTable as well as reading data from a loaded Dictionary and HashTable. You can see the performance difference from the graph. After every execution you may find different data but the curve of the graph will remain the same. From the given analysis we can conclude that Dictionary performs well even for large data.

Using the code

The below code with comments is used to check the performance of the HashTable and Dictionary objects.

C#
[TestClass]
public class MyTestClass
{
    private TestContext testContextInstance;
    public TestContext TestContext
    {
        get { return testContextInstance; }
        set { testContextInstance = value; }
    }

    [TestMethod]
    [DeploymentItem("SampleData.xml")]
    [DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML", 
      "|DataDirectory|\\SampleData.xml", 
      "Row", DataAccessMethod.Sequential)]
    public void MyTestMethod1()
    {
        int value = Convert.ToInt32(testContextInstance.DataRow["Data"]);
        Stopwatch watch = new Stopwatch();

        // Testing Dictionary for Reading and writing.            
        watch.Restart();
        var dictionary = LoadDataTestForDictionary(value);
        watch.Stop();
        System.Diagnostics.Trace.WriteLine("Elapsed Milliseconds(Load Dictionary): " + 
          watch.ElapsedMilliseconds.ToString());

        watch.Restart();
        ReadEachElementOfDictionary(dictionary);
        watch.Stop();
        System.Diagnostics.Trace.WriteLine("Elapsed Milliseconds(Read Dictionary): " + 
          watch.ElapsedMilliseconds.ToString());

        // Testing HashTable for Reading and writing.
        watch.Restart();
        var hash = LoadDataTestForHashTable(value);
        watch.Stop();
        System.Diagnostics.Trace.WriteLine("Elapsed Milliseconds(Load HashTable): " + 
          watch.ElapsedMilliseconds.ToString());

        watch.Restart();
        ReadEachElementOfHashTable(hash);
        watch.Stop();
        System.Diagnostics.Trace.WriteLine("Elapsed Milliseconds(Read HashTable): " + 
          watch.ElapsedMilliseconds.ToString());
    }

    public Dictionary<int, string> LoadDataTestForDictionary(int totalRecords)
    {
        // Create Dictionary object.
        Dictionary<int, string> dict = new Dictionary<int, string>();

        for (int i = 0; i < totalRecords; i++)
        {
            dict.Add(i, "this is test data");
        }

        return dict;
    }

    public void ReadEachElementOfDictionary(Dictionary<int, string> dict)
    {
        for (int i = 0; i < dict.Count; i++)
        {
            var value = dict[i];
        }
    }

    public Hashtable LoadDataTestForHashTable(int totalRecords)
    {            
        Hashtable hTable = new Hashtable();

        for (int i = 0; i < totalRecords; i++)
        {
            hTable.Add(i, "this is test data");
        }

        return hTable;
    }

    public void ReadEachElementOfHashTable(Hashtable htable)
    {
        for (Int64 i = 0; i < htable.Count; i++)
        {
            var value = htable[i];
        }
    }
} 

To test the above code, you need an XML file to load the data. You can also modify the XML file to test for different data.

XML
<?xml version="1.0" encoding="utf-8" ?>
<Rows>
  <Row>
    <Data>100000</Data>
  </Row>
  <Row>
    <Data>500000</Data>
  </Row>
  <Row>
    <Data>1000000</Data>
  </Row>
  <Row>
    <Data>5000000</Data>
  </Row>
  <Row>
    <Data>10000000</Data>
  </Row> 
</Rows> 

Output/Result

Result data that we have taken as sample:

Image 1

Result data in graph form (row number vs. milliseconds):

Image 2

History

  • 23 Sep. 2013: Initial article. But as per comments from Robert, I felt my result is incorrect.
  • 24 Sep. 2013: Updated testing methodology to check for performance.

Disclaimer

There is no guarantee for the given analysis. Try yourself to consider it as proof.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)