For more information navigate to: Stopwatch Class.
Following example demonstrates how to use Stopwatch Class to determine execution time for an application. Using this example you can see differences in efficiency by calling Contains method of StringCollection and HashTable.
Hashtable hs = new Hashtable(); StringCollection sc = new StringCollection(); for (int i = 0; i < int.MaxValue/1000; i++) { hs.Add(i.ToString(), i); sc.Add(i.ToString()); } Random rnd = new Random(); Stopwatch timer = new Stopwatch(); int val = rnd.Next(int.MaxValue / 1000); timer.Start(); if (sc.Contains(val.ToString())) { Console.WriteLine("Contains"); } timer.Stop(); Console.WriteLine(timer.ElapsedTicks); timer.Reset(); timer.Start(); if (hs.Contains(val.ToString())) { Console.WriteLine("Contains"); } timer.Stop(); Console.WriteLine(timer.ElapsedTicks);
No comments:
Post a Comment