首页 > 代码库 > C# 集合性能比较(代码测试)
C# 集合性能比较(代码测试)
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(); } }}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。