Jūs esateŽurnalai / Ernestas Kardzys's blog / C#. Int array vs. ArrayList vs. Dictionary

C#. Int array vs. ArrayList vs. Dictionary


ParašėErnestas Kardzys - 2009 Birželio 07

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 :)

Hey, simi ;]

Thanks for your note && links ;)

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

Skelbti naują komentarą

Šio laukelio turinys bus laikomas privatus ir nerodomas viešai.
  • Web puslapiu adresai ir el. pašto adresai automatiškai tampa nuorodomis.
  • Leidžiamos HTML žymės: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Linijos ir paragrafai atskiriami automatiškai
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <php>, <python>, <ruby>. The supported tag styles are: <foo>, [foo].

Daugiau informacijos apie teksto formatavimą

CAPTCHA
Šis klausimas yra skirtas įsitikinti, jog jūs esate žmogus, ir sustabdyti automatinį šlamšto siuntimą.