Friday, January 18, 2008

Fastest Way to find an object that contains a property in an ArrayList

Dont use an ArrayList implmente IDictonary or ICollection so you can
access with col[YourKey]



   1:  using System;

   2:  using System.Collections;

   3:  using System.Collections.Generic;

   4:   

   5:  public class MyClass

   6:  {

   7:      public class TestClass

   8:      {

   9:          public string MyName = string.Empty;

  10:          public TestClass(string name)

  11:          {

  12:              this.MyName = name;

  13:          }

  14:      }

  15:      public static void Main()

  16:      {

  17:          Hashtable ht = new Hashtable();

  18:          string [] findKey = new string[] { 

  19:                  new Random().Next(1, 1000).ToString(),

  20:                  new Random().Next(1, 1000).ToString(),

  21:                  new Random().Next(1, 1000).ToString(),

  22:                  new Random().Next(1, 1000).ToString(),

  23:                  new Random().Next(1, 1000).ToString(),

  24:                  new Random().Next(1, 1000).ToString(),

  25:                  new Random().Next(1, 1000).ToString(),

  26:                  new Random().Next(1, 1000).ToString(),

  27:                  new Random().Next(1, 1000).ToString(),

  28:                  new Random().Next(1, 1000).ToString(),

  29:                  new Random().Next(1, 1000).ToString(),

  30:                  new Random().Next(1, 1000).ToString(),

  31:                  new Random().Next(1, 1000).ToString(),

  32:                  new Random().Next(1, 1000).ToString(),

  33:                  new Random().Next(1, 1000).ToString(),

  34:                  new Random().Next(1, 1000).ToString(),

  35:                  new Random().Next(1, 1000).ToString(),

  36:                  new Random().Next(1, 1000).ToString(),

  37:                  new Random().Next(1, 1000).ToString(),

  38:                  new Random().Next(1, 1000).ToString(),

  39:                  new Random().Next(1, 1000).ToString(),

  40:                  new Random().Next(1, 1000).ToString(),

  41:                  new Random().Next(1, 1000).ToString(),

  42:                  new Random().Next(1, 1000).ToString(),

  43:                  new Random().Next(1, 1000).ToString(),

  44:                  new Random().Next(1, 1000).ToString(),

  45:                  new Random().Next(1, 1000).ToString(),

  46:                  new Random().Next(1, 1000).ToString(),

  47:                  new Random().Next(1, 1000).ToString(),

  48:                  new Random().Next(1, 1000).ToString(),

  49:                  new Random().Next(1, 1000).ToString(),

  50:                  new Random().Next(1, 1000).ToString(),

  51:                  new Random().Next(1, 1000).ToString(),

  52:                  new Random().Next(1, 1000).ToString(),

  53:                  new Random().Next(1, 1000).ToString(),

  54:                  new Random().Next(1, 1000).ToString(),

  55:                  new Random().Next(1, 1000).ToString(),

  56:                  new Random().Next(1, 1000).ToString(),

  57:                  new Random().Next(1, 1000).ToString(),

  58:                  new Random().Next(1, 1000).ToString(),

  59:                  new Random().Next(1, 1000).ToString(),

  60:                  new Random().Next(1, 1000).ToString(), 

  61:                  new Random().Next(1, 1000).ToString()

  62:              };        

  63:          

  64:          for(int i = 0; i< 1000; i++)

  65:          {

  66:              TestClass t = new TestClass(i.ToString());

  67:              ht.Add(i.ToString(),t);

  68:          }

  69:          DateTime startTime = DateTime.Now;

  70:          foreach(string abc in findKey)

  71:          foreach(DictionaryEntry de in ht)

  72:          {

  73:              if(de.Key == abc)

  74:              {

  75:                  TestClass tt = de.Value as TestClass;

  76:                  Console.WriteLine(tt.MyName);

  77:                  break;

  78:              }            

  79:          }

  80:          

  81:          DateTime stopTime = DateTime.Now;

  82:          TimeSpan duration = stopTime - startTime;

  83:   

  84:          Console.Write(@"foreach took (ms): ");

  85:          Console.WriteLine(duration.Milliseconds);

  86:          startTime = DateTime.Now;

  87:          foreach(string abc in findKey)

  88:          {

  89:              TestClass tt1 = ht[abc] as TestClass;

  90:              Console.WriteLine(tt1.MyName);

  91:          }        

  92:          

  93:          stopTime = DateTime.Now;

  94:          duration = stopTime - startTime;

  95:          

  96:          Console.Write(@"key took (ms): ");

  97:          Console.WriteLine(duration.Milliseconds);        

  98:          

  99:          Console.ReadLine();

 100:      }

 101:  }

No comments:

Shared Cache - .Net Caching made easy

All information about Shared Cache is available here: http://www.sharedcache.com/. Its free and easy to use, we provide all sources at codeplex.

Facebook Badge