I was under the impression that it was quicker to look up items in a Dictionary than a List, the follow code seems to suggest otherwise:
Dictionary : 66 ticks
List: 32 ticks
Im assuming ive screwed up somewhere?
static void Main(string[] args)
{
// Speed test.
Dictionary<string, int> d = new Dictionary<string, int>()
{
{"P1I1-1MS P2I1-1MS 3I-1MS 4I-1MS", 2},
{"P1I2-1MS P2I1-1MS 3I-1MS 4I-1MS", 1},
{"P1I3-1MS P2I1-1MS 3I-1MS 4I-1MS", 0},
{"P1I4-1MS P2I1-1MS 3I-1MS 4I-1MS", -1},
{"P1I5-1MS P2I1-1MS 3I-1MS 4I-1MS", 0},
{"P1I1-1MS P2I2-1MS 3I-1MS 4I-1MS", 0},
{"P1I2-1MS P2I2-1MS 3I-1MS 4I-1MS", 0},
{"P1I3-1MS P2I2-1MS 3I-1MS 4I-1MS", 0},
{"P1I4-1MS P2I2-1MS 3I-1MS 4I-1MS", 0},
{"P1I5-1MS P2I2-1MS 3I-1MS 4I-1MS", 0},
{"P1I1-1MS P2I3-1MS 3I-1MS 4I-1MS", 0},
{"P1I2-1MS P2I3-1MS 3I-1MS 4I-1MS", 0},
{"P1I3-1MS P2I3-1MS 3I-1MS 4I-1MS", 0},
{"P1I4-1MS P2I3-1MS 3I-1MS 4I-1MS", 0},
{"P1I5-1MS P2I3-1MS 3I-1MS 4I-1MS", 0},
{"P1I1-1MS P2I4-1MS 3I-1MS 4I-1MS", 2}
};
List<string> l = new List<string>();
l.Add("P1I1-1MS P2I1-1MS 3I-1MS 4I-1MS");
l.Add("P1I2-1MS P2I1-1MS 3I-1MS 4I-1MS");
l.Add("P1I3-1MS P2I1-1MS 3I-1MS 4I-1MS");
l.Add("P1I4-1MS P2I1-1MS 3I-1MS 4I-1MS");
l.Add("P1I5-1MS P2I1-1MS 3I-1MS 4I-1MS");
l.Add("P1I1-1MS P2I2-1MS 3I-1MS 4I-1MS");
l.Add("P1I2-1MS P2I2-1MS 3I-1MS 4I-1MS");
l.Add("P1I3-1MS P2I2-1MS 3I-1MS 4I-1MS");
l.Add("P1I4-1MS P2I2-1MS 3I-1MS 4I-1MS");
l.Add("P1I5-1MS P2I2-1MS 3I-1MS 4I-1MS");
l.Add("P1I1-1MS P2I3-1MS 3I-1MS 4I-1MS");
l.Add("P1I2-1MS P2I3-1MS 3I-1MS 4I-1MS");
l.Add("P1I3-1MS P2I3-1MS 3I-1MS 4I-1MS");
l.Add("P1I4-1MS P2I3-1MS 3I-1MS 4I-1MS");
l.Add("P1I5-1MS P2I3-1MS 3I-1MS 4I-1MS");
l.Add("P1I1-1MS P2I4-1MS 3I-1MS 4I-1MS");
Stopwatch sw = new Stopwatch();
string temp = "P1I1-1MS P2I4-1MS 3I-1MS 4I-1MS";
bool inDictionary = false;
sw.Start();
if (d.ContainsKey(temp))
{
sw.Stop();
inDictionary = true;
}
else sw.Reset();
Console.WriteLine(sw.ElapsedTicks.ToString());
Console.WriteLine(inDictionary.ToString());
bool inList = false;
sw.Reset();
sw.Start();
if (l.Contains(temp))
{
sw.Stop();
inList = true;
}
else sw.Reset();
Console.WriteLine(sw.ElapsedTicks.ToString());
Console.WriteLine(inList.ToString());
Console.ReadLine();
}
EDIT
Modification to Matthew Watson's code.