We all know how useful a hash table can be, but if you are using the .NET Framework 2.0 you should be using a Dictionary instead. A dictionary and a hash table in .NET 2.0 are very similar but with 1 main difference, a dictionary is faster because it does not need to box and unbox the data as a hash table would.
I made a simple program to see just how much faster a dictionary is than a hash table. I ran the test 20 times for each data set and the results were close each time. A GUID as the key and a boolean as the data.
This is the results of the last test for each dataset.
My comp specs
Vista Business 32bit, AMD 64 x2 5000+, 2GB ram
and now for the code I used. You can also download the project.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
namespace hashVsDictionary
{
internal class Program
private static void Main(string[] args)
#region init
int num = 10000000;
Guid[] guids = new Guid[num];
Stopwatch s = new Stopwatch();
s.Start();
for (int i = 0; i < num; i++)
guids[i] = Guid.NewGuid();
}
s.Stop();
Console.WriteLine("Data Creation = " + s.Elapsed);
s.Reset();
#endregion
#region hash
Hashtable hash = new Hashtable();
hash.Add(guids[i], true);
Console.WriteLine("Insert Hash = " + s.Elapsed);
bool b = Convert.ToBoolean(hash[guids[i]]);
Console.WriteLine("Extracting Hash = " + s.Elapsed);
hash.Remove(guids[i]);
Console.WriteLine("Removing Hash = " + s.Elapsed);
#region dict
Dictionary<Guid, bool> dict = new Dictionary<Guid, bool>();
dict.Add(guids[i], true);
Console.WriteLine("Insert Dictionary = " + s.Elapsed);
bool b = dict[guids[i]];
Console.WriteLine("Extracting Dict = " + s.Elapsed);
dict.Remove(guids[i]);
Console.WriteLine("Removing Dict = " + s.Elapsed);
Console.ReadLine();
Remember Me
a@href@title, b, i, strike, strong, u
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.