using System; using System.Collections; using System.Collections.Generic; using System.Data; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Table_And_List { public class Program { public static void Main(string[] args) { int total = 100000; int[] x = new int[9999999]; for (int i = 0; i < x.Length; i++) { x[i] = i; } int[] x2 = new int[] { 6666, 333, 11111, 56764, 33335 }; int x3 = new Random().Next(total); SortedDictionaryTest(x, x2, x3); DictionaryTest(x, x2, x3); SortedListTest(x, x2, x3); HashtableTest(x, x2, x3); Console.ReadKey(); } private static void SortedListTest(int[] x, int[] x2, int x3) { var ht = new SortedList(); Stopwatch watch = new Stopwatch(); watch.Start(); foreach (var item in x) { ht.Add(item, item); } watch.Stop(); Console.WriteLine(string.Format("SortedList添加{0}个元素耗时:{1}ms", x.Length, watch.ElapsedMilliseconds)); watch.Reset(); watch.Start(); foreach (var item in x2) { //Console.WriteLine("value:{0},?{1}",item,ht.ContainsKey(item)); ht.ContainsValue(item); } watch.Stop(); Console.WriteLine(string.Format("SortedList查找在100000个元素中查找随机的{0}个的元素耗时:{1}ms", x2.Length, watch.ElapsedMilliseconds)); ht.Clear(); } private static void HashtableTest(int[] x, int[] x2, int x3) { Hashtable ht = new Hashtable(); Stopwatch watch = new Stopwatch(); watch.Start(); foreach (var item in x) { ht.Add(item, item); } watch.Stop(); Console.WriteLine(string.Format("Hashtable添加{0}个元素耗时:{1}ms", x.Length, watch.ElapsedMilliseconds)); watch.Reset(); watch.Start(); foreach (var item in x2) { //Console.WriteLine("value:{0},?{1}",item,ht.ContainsKey(item)); //ht.ContainsKey(item); ht.ContainsValue(item); } watch.Stop(); Console.WriteLine(string.Format("Hashtable查找在100000个元素中查找随机的{0}个的元素耗时:{1}ms", x2.Length, watch.ElapsedMilliseconds)); ht.Clear(); } private static void DictionaryTest(int[] x, int[] x2, int x3) { Dictionary<int, int> ht = new Dictionary<int, int>(); Stopwatch watch = new Stopwatch(); watch.Start(); foreach (var item in x) { ht.Add(item, item); } watch.Stop(); Console.WriteLine(string.Format("Dictionary添加{0}个元素耗时:{1}ms", x.Length, watch.ElapsedMilliseconds)); watch.Reset(); watch.Start(); foreach (var item in x2) { //Console.WriteLine("value:{0},?{1}", item, ht.ContainsKey(item)); //ht.ContainsKey(item); ht.ContainsValue(item); } watch.Stop(); Console.WriteLine(string.Format("Dictionary查找在100000个元素中查找随机的{0}个的元素耗时:{1}ms", x2.Length, watch.ElapsedMilliseconds)); ht.Clear(); } private static void SortedDictionaryTest(int[] x, int[] x2, int x3) { SortedDictionary<int, int> ht = new SortedDictionary<int, int>(); Stopwatch watch = new Stopwatch(); watch.Start(); foreach (var item in x) { ht.Add(item, item); } watch.Stop(); Console.WriteLine(string.Format("SortedDictionary添加{0}个元素耗时:{1}ms", x.Length, watch.ElapsedMilliseconds)); watch.Reset(); watch.Start(); foreach (var item in x2) { //Console.WriteLine("value:{0},?{1}", item, ht.ContainsKey(item)); //ht.ContainsKey(item); ht.ContainsValue(item); } watch.Stop(); Console.WriteLine(string.Format("SortedDictionary查找在100000个元素中查找随机的{0}个的元素耗时:{1}ms", x2.Length, watch.ElapsedMilliseconds)); ht.Clear(); } } }
转载于:https://www.cnblogs.com/mingxuantongxue/p/3911316.html