Thursday, June 21, 2007

Indexing by integer: Array vs List vs Dictionary

I really wanted to find out about the performance of an array as compared to a List and a dictionary.
So i just wrote something to check it. (See the results below:)

using System;
using System.Collections.Generic;
namespace ConsoleApplication1
{
class LookupPerformanceTest
{
static void Main(string[] args)
{
// ten million
int iterations = 10000000;
string[] array = new string[iterations];
List list = new List(iterations);
Dictionary dictionary = new Dictionary(iterations);
int now = Environment.TickCount;
for (int i = 0; i < now =" Environment.TickCount;" i =" 0;" now =" Environment.TickCount;" i =" 0;" now =" Environment.TickCount;" i =" 0;" s =" array[i];" now =" Environment.TickCount;" i =" 0;" s =" list[i];" now =" Environment.TickCount;" i =" 0;" s =" dictionary[i];">


Results (running in Release mode)
Array population took: 94ms
List population took: 141ms
Dictionary population took: 719ms
Array lookup took: 15ms
List lookup took: 32ms
Dictionary lookup took: 593ms