TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Array and Dictionary Test, Integer Lookups

This C# program compares 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.

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.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf