C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Important: Every lookup has to compute the hash code, which has a performance penalty. The TryGetValue method can help here.
Value: Look carefully at "value" in this program. It is accessed by TryGetValue, and we do not need to get it again when we use it.
C# program that uses TryGetValue
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
var counts = new Dictionary<string, int>();
counts.Add("key", 0);
// Keep the result of TryGetValue.
int value;
if (counts.TryGetValue("key", out value))
{
counts["key"] = value + 1;
Console.WriteLine("VALUE: " + counts["key"]);
}
}
}
Output
VALUE: 1
And: This is inefficient. Unlike the version with TryGetValue, we do an extra lookup to get "value" again.
C# program that uses ContainsKey
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
var counts = new Dictionary<string, int>();
counts.Add("key", 0);
// This causes an unneeded look up.
if (counts.ContainsKey("key"))
{
counts["key"]++;
Console.WriteLine("VALUE: " + counts["key"]);
}
}
}
Output
VALUE: 1
C# program that shows what ContainsKey does
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
var counts = new Dictionary<string, int>();
counts.Add("key", 0);
// This causes extra lookups.
if (counts.ContainsKey("key"))
{
counts["key"] = counts["key"] + 1;
Console.WriteLine("VALUE: " + counts["key"]);
}
}
}
Output
VALUE: 1
Version 1: Use ContainsKey. If the key exists, access the value again in a separate lookup statement.
Version 2: This version of the code uses TryGetValue, and stores the result value which is then used for the sum.
Result: It is faster to just use the TryGetValue method and access the already-known value from a local variable.
C# program that benchmarks ContainsKey and TryGetValue
using System;
using System.Collections.Generic;
using System.Diagnostics;
class Program
{
const int _max = 10000000;
static void Main()
{
var test = new Dictionary<string, int>();
test["key"] = 1;
int sum = 0;
// Version 1: use ContainsKey and access the key again for its value.
var s1 = Stopwatch.StartNew();
for (int i = 0; i < _max; i++)
{
if (test.ContainsKey("key"))
{
sum += test["key"];
}
}
s1.Stop();
// Version 2: use TryGetValue and use the key already accessed.
var s2 = Stopwatch.StartNew();
for (int i = 0; i < _max; i++)
{
if (test.TryGetValue("key", out int result))
{
sum += result;
}
}
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"));
}
}
Output
38.11 ns ContainsKey, indexer
21.16 ns TryGetValue