Pagrindinis > Programavimas > C#. Int array vs. ArrayList vs. Dictionary

C#. Int array vs. ArrayList vs. Dictionary

Birželis 7th, 2009 Ernestas Kardzys Komentuoti Komentarai

private static void TestIntArray()
{
int [] array = new int[1000];
for (int i = 0; i < 1000; i++)
array[i] = i;
}

private static void TestArrayList()
{
ArrayList array = new ArrayList();
for (int i = 0; i < 1000; i++)
array.Add(i);
}

// Code….

long start = DateTime.Now.Ticks;
TestIntArray();
long end = DateTime.Now.Ticks;
Console.WriteLine(“Time with int array: {0}”, end – start);

long start2 = DateTime.Now.Ticks;
TestArrayList();
long end2 = DateTime.Now.Ticks;
Console.WriteLine(“Time with ArrayList array: {0}”, end2 – start2);

The result:

Time with int array: 30000
Time with ArrayList array: 10000

So, working with ArrayList is faster. Hm…

If we have multidimensional array and Dictionary<int, int> the difference is even bigger:

private static void TestIntArray()
{
int [ , ] array = new int[1000, 1000];
int i = 0;
for (int j = 0; j < 1000; j++)
array[i , j] = i++;
}

private static void TestDictionary()
{
Dictionary<int, int> array = new Dictionary<int, int>();
int i = 0;
for (int j = 0; j < 1000; j++)
array.Add(j, i++);
}

Time with int array: 60000
Time with Dictionary: 10000

Interesting :)

  1. Gruodis 30th, 2009 į 09:00 | #1

    hi,

    First of all. Thanks very much for your useful post.

    I just came across your blog and wanted to drop you a note telling you how impressed I was with the information you have posted here.

    Please let me introduce you some info related to this post and I hope that it is useful for .Net community.

    There is a good C# resource site, Have alook

    http://www.csharptalk.com/2009/09/c-array.html
    http://www.csharptalk.com/2009/10/creating-arrays.html

    simi

  2. Gruodis 30th, 2009 į 19:19 | #2

    Hey, simi ;]

    Thanks for your note && links ;)

  1. Citatų nėra.