Mega Code Archive

 
Categories / C# / Collections Data Structure
 

Compares the efficiency of the hash map implementation with the tree map

using System; using System.Collections; public class CompareMaps {   public const int MAX  = 50000000;   public static String time(IDictionary map) {     Object value = null;     long starttime = Environment.TickCount;     for (int i = 0; i < MAX; i++) {         value = map[i];     }      long stoptime = Environment.TickCount;     return value + " took " + (stoptime - starttime);   }                public static void Main(String[] args) {     int SIZE = 1000;     IDictionary hash = new Hashtable(2*SIZE);     IDictionary tree = new SortedList();          Random random = new Random();     int i = random.Next(5000000);          for (int j = 0; j < SIZE; j++) {        if(!hash.Contains(i))           hash.Add(i, i);        if(!tree.Contains(i))           tree.Add(i, i);     }     Console.WriteLine("Hash for {0}", time(hash));     Console.WriteLine("Tree for {0}", time(tree));   } }