C# Array and Dictionary Test, Integer Lookups

Compare the integer lookup performance of the array and Dictionary types.

Array, Dictionary test. Arrays and Dictionaries can be accessed with int keys. With the Dictionary, you will have hashcode computations and lookups. With the array you will have computed indices. This C# experiment benchmarks lookups.ArrayDictionaryOptimization

First, we see a program written in the C# language that generates an array of strings with 1000 values. It also creates a Dictionary with 1000 integer keys and string values, using the default IEqualityComparer<int>.IEqualityComparer
Properties: There are 2 properties called ArrayTest and DictionaryTest that return the data structures.
Important: In this specific case, the two collections have basically the same purpose and usage.
Info: This code creates an array and a Dictionary that both contain strings corresponding to the keys or offsets 0-999.
And: The program finds that the values of the strings at the 100 key or offset are equal.
C# program that uses array, Dictionary using System; using System.Diagnostics; using System.Collections.Generic; class Program { static string[] ArrayTest { get { string[] array = new string[1000]; for (int i = 0; i < 1000; i++) { array[i] = i.ToString(); } return array; } } static Dictionary<int, string> DictionaryTest { get { var dictionary = new Dictionary<int, string>(); for (int i = 0; i < 1000; i++) { dictionary.Add(i, i.ToString()); } return dictionary; } } static void Main() { // // Get the array and Dictionary. // var array = ArrayTest; var dictionary = DictionaryTest; // // Lookup a value at this index and compare. // string value1 = array[100]; string value2 = dictionary[100]; Console.WriteLine(value1); Console.WriteLine(value2); Console.WriteLine(value1 == value2); } } Output 100 100 True

Benchmark. First, as a developer you will consider it very likely that the array lookup will be much faster than the Dictionary key computation. This is entirely correct. But this test can give you an idea of the difference between the two lookups.
Also: This article can point you in the direction of how you can exchange a Dictionary for an array.
Code that uses array and Dictionary: C# // Array block string value = array[i % 1000]; if (string.IsNullOrEmpty(value)) { throw new Exception(); } // Dictionary block string value = dictionary[i % 1000]; if (string.IsNullOrEmpty(value)) { throw new Exception(); } Benchmark description Iterations tested: 100000000 Loop type: for loop Array lookup: 367 ms Dictionary lookup: 2419 ms

Discussion. The most important part of this article is just to remind us that sometimes, arrays and Dictionaries are interchangeable. And there are advantages to using simpler data structures such as arrays.
Often: We can assign static resources such as images or files to a certain numeric value.
Then: We can store these static resources in an array instead of a Dictionary with int keys.

Summary. We exchanged a Dictionary or Hashtable data structure for an array reference type. This has performance and memory usage benefits that are significant. These factors can add up in a complex or performance-critical application.

© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.