Click here to Skip to main content
15,881,559 members
Articles / General Programming / Performance
Tip/Trick

Performance measurement of HashTable vs. Dictionary

Rate me:
Please Sign up or sign in to vote.
4.86/5 (10 votes)
24 Sep 2013CPOL1 min read 67K   9   14
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)


Written By
Software Developer (Senior)
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralHashtable uses boxing/unboxing, Dictionary does not Pin
Robert Friberg11-Oct-16 22:19
Robert Friberg11-Oct-16 22:19 
GeneralRe: Hashtable uses boxing/unboxing, Dictionary does not Pin
AmitGajjar12-Oct-16 10:37
professionalAmitGajjar12-Oct-16 10:37 
Thank you for your inputs. Smile | :)
QuestionMy Vote is -1 Pin
Hassan Alrehamy26-Apr-15 10:35
Hassan Alrehamy26-Apr-15 10:35 
QuestionMy vote of 5 Pin
Erich Ledesma27-Nov-13 0:36
Erich Ledesma27-Nov-13 0:36 
AnswerRe: My vote of 5 Pin
AmitGajjar27-Nov-13 0:42
professionalAmitGajjar27-Nov-13 0:42 
GeneralThanks Matt T Heffron and Robert Rohde Pin
_Nizar23-Sep-13 21:17
_Nizar23-Sep-13 21:17 
General[My vote of 1] There is lots wrong with this benchmarking Pin
Matt T Heffron23-Sep-13 7:16
professionalMatt T Heffron23-Sep-13 7:16 
GeneralRe: [My vote of 1] There is lots wrong with this benchmarking Pin
AmitGajjar23-Sep-13 18:43
professionalAmitGajjar23-Sep-13 18:43 
GeneralRe: [My vote of 1] There is lots wrong with this benchmarking Pin
AmitGajjar23-Sep-13 20:39
professionalAmitGajjar23-Sep-13 20:39 
GeneralRe: [My vote of (1) -> 4] There is lots wrong with this benchmarking Pin
Matt T Heffron24-Sep-13 6:42
professionalMatt T Heffron24-Sep-13 6:42 
GeneralRe: [My vote of (1) -> 4] There is lots wrong with this benchmarking Pin
AmitGajjar24-Sep-13 19:27
professionalAmitGajjar24-Sep-13 19:27 
GeneralMy vote of 1 Pin
Robert Rohde23-Sep-13 3:23
Robert Rohde23-Sep-13 3:23 
GeneralRe: My vote of 1 Pin
AmitGajjar23-Sep-13 18:42
professionalAmitGajjar23-Sep-13 18:42 
GeneralRe: My vote of 1 Pin
AmitGajjar23-Sep-13 20:42
professionalAmitGajjar23-Sep-13 20:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.