c# – What is more efficient: Dictionary TryGetValue or ContainsKey+Item?

c# – What is more efficient: Dictionary TryGetValue or ContainsKey+Item?

TryGetValue will be faster.

ContainsKey uses the same check as TryGetValue, which internally refers to the actual entry location. The Item property actually has nearly identical code functionality as TryGetValue, except that it will throw an exception instead of returning false.

Using ContainsKey followed by the Item basically duplicates the lookup functionality, which is the bulk of the computation in this case.

A quick benchmark shows that TryGetValue has a slight edge:

    static void Main() {
        var d = new Dictionary<string, string> {{a, b}};
        var start = DateTime.Now;
        for (int i = 0; i != 10000000; i++) {
            string x;
            if (!d.TryGetValue(a, out x)) throw new ApplicationException(Oops);
            if (d.TryGetValue(b, out x)) throw new ApplicationException(Oops);
        }
        Console.WriteLine(DateTime.Now-start);
        start = DateTime.Now;
        for (int i = 0; i != 10000000; i++) {
            string x;
            if (d.ContainsKey(a)) {
                x = d[a];
            } else {
                x = default(string);
            }
            if (d.ContainsKey(b)) {
                x = d[b];
            } else {
                x = default(string);
            }
        }
   }

This produces

00:00:00.7600000
00:00:01.0610000

making the ContainsKey + Item access about 40% slower assuming an even blend of hits and misses.

Moreover, when I change the program to always miss (i.e. always looking up b) the two versions become equally fast:

00:00:00.2850000
00:00:00.2720000

When I make it all hits, however, the TryGetValue remains a clear winner:

00:00:00.4930000
00:00:00.8110000

c# – What is more efficient: Dictionary TryGetValue or ContainsKey+Item?

Since none of the answers thus far actually answer the question, here is an acceptable answer I found after some research:

If you decompile TryGetValue you see that it’s doing this:

public bool TryGetValue(TKey key, out TValue value)
{
  int index = this.FindEntry(key);
  if (index >= 0)
  {
    value = this.entries[index].value;
    return true;
  }
  value = default(TValue);
  return false;
}

whereas the ContainsKey method is:

public bool ContainsKey(TKey key)
{
  return (this.FindEntry(key) >= 0);
}

so TryGetValue is just ContainsKey plus an array lookup if the item is present.

Source

It appears that TryGetValue will be almost twice as fast as ContainsKey+Item combination.

Leave a Reply

Your email address will not be published.