TheDeveloperBlog.com


C# Array and Dictionary Test, Integer Lookups

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.

Array and Dictionary performance

Array lookup:       367 ms
Dictionary lookup: 2419 ms


Example. 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

Next, there are two properties called ArrayTest and DictionaryTest that return the data structures. In this specific case, the two collections have basically the same purpose and usage.

C# program that uses array and 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

This code creates an array and a Dictionary that both contain strings corresponding to the keys or offsets 0-999. The program finds that the values of the strings at the 100 key or offset are equal.


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.

Benchmark Programs
Code that uses array and Dictionary: C#
    (Each block is tested in a loop.)

// 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
    (See top figures.)

Iterations tested: 100000000
Loop type:         for loop


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.