We test the Hashtable collection against the Dictionary. The benchmark first populates an equivalent version of each collection.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
const int _max = 1000000;
// Set up collections.
Hashtable hashtable = new Hashtable();
for (int i = 0; i <= 30000; i++)
{
hashtable[i.ToString(
"00000")] = i;
}
var dictionary = new Dictionary<string, int>();
for (int i = 0; i <= 30000; i++)
{
dictionary.Add(i.ToString(
"00000"), i);
}
// Version 1: use Hashtable.
var s1 = Stopwatch.StartNew();
for (int i = 0; i < _max; i++)
{
if (!hashtable.ContainsKey(
"09999") || !hashtable.ContainsKey(
"30000"))
{
return;
}
}
s1.Stop();
// Version 2: use Dictionary.
var s2 = Stopwatch.StartNew();
for (int i = 0; i < _max; i++)
{
if (!dictionary.ContainsKey(
"09999") || !dictionary.ContainsKey(
"30000"))
{
return;
}
}
s2.Stop();
Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000000) / _max).ToString(
"0.00 ns"));
Console.WriteLine(((double)(s2.Elapsed.TotalMilliseconds * 1000000) / _max).ToString(
"0.00 ns"));
66.99 ns Hashtable
51.22 ns Dictionary