C#中字典集合HashTable、Dictionary、ConcurrentDictionary三者区别
2016-09-03 10:57
387 查看
学习参考博客:http://www.cnblogs.com/yinrq/p/5584885.html
使用Stopwatch类测试耗时代码:
最后总结:
大数据插入Dictionary花费时间最少
遍历HashTable最快是Dictionary的1/5,ConcurrentDictionary的1/10
单线程建议用Dictionary,多线程建议用ConcurrentDictionary或者HashTable(Hashtable tab = Hashtable.Synchronized(new Hashtable());获得线程安全的对象)
使用Stopwatch类测试耗时代码:
using System; using System.Collections; using System.Collections.Concurrent; using System.Collections.Generic; using System.Diagnostics; namespace WebApp { class Program { static Hashtable _hashtable; static Dictionary<string, string> _dictionary; static ConcurrentDictionary<string, string> _conDictionary; static void Main(string[] args) { Compare(5000000); Console.ReadLine(); Console.Read(); } public static void Compare(int dataCount) { _hashtable = new Hashtable(); _dictionary = new Dictionary<string, string>(); _conDictionary = new ConcurrentDictionary<string, string>(); Stopwatch stopWatch = new Stopwatch(); // Hashtable stopWatch.Start(); for (int i = 0; i < dataCount; i++) { _hashtable.Add("key" + i.ToString(), "Value" + i.ToString()); } stopWatch.Stop(); Console.WriteLine("HashTable插" + dataCount + "条耗时(毫秒):" + stopWatch.ElapsedMilliseconds); //Dictionary stopWatch.Reset(); stopWatch.Start(); for (int i = 0; i < dataCount; i++) { _dictionary.Add("key" + i.ToString(), "Value" + i.ToString()); } stopWatch.Stop(); Console.WriteLine("Dictionary插" + dataCount + "条耗时(毫秒):" + stopWatch.ElapsedMilliseconds); //ConcurrentDictionary stopWatch.Reset(); stopWatch.Start(); for (int i = 0; i < dataCount; i++) { _conDictionary.TryAdd("key" + i.ToString(), "Value" + i.ToString()); } stopWatch.Stop(); Console.WriteLine("ConcurrentDictionary插" + dataCount + "条耗时(毫秒):" + stopWatch.ElapsedMilliseconds); // Hashtable stopWatch.Reset(); stopWatch.Start(); for (int i = 0; i < _hashtable.Count; i++) { var key = _hashtable[i]; } stopWatch.Stop(); Console.WriteLine("HashTable遍历时间(毫秒):" + stopWatch.ElapsedMilliseconds); //Dictionary stopWatch.Reset(); stopWatch.Start(); for (int i = 0; i < _hashtable.Count; i++) { var key = _dictionary["key" + i.ToString()]; } stopWatch.Stop(); Console.WriteLine("Dictionary遍历时间(毫秒):" + stopWatch.ElapsedMilliseconds); //ConcurrentDictionary stopWatch.Reset(); stopWatch.Start(); for (int i = 0; i < _hashtable.Count; i++) { var key = _conDictionary["key" + i.ToString()]; } stopWatch.Stop(); Console.WriteLine("ConcurrentDictionary遍历时间(毫秒):" + stopWatch.ElapsedMilliseconds); } } }
最后总结:
大数据插入Dictionary花费时间最少
遍历HashTable最快是Dictionary的1/5,ConcurrentDictionary的1/10
单线程建议用Dictionary,多线程建议用ConcurrentDictionary或者HashTable(Hashtable tab = Hashtable.Synchronized(new Hashtable());获得线程安全的对象)
相关文章推荐
- C#.NET集合类型Hashtable与Dictionary 的区别
- C#中字典集合HashTable、Dictionary、ConcurrentDictionary三者区别
- C#基础(18)——List泛型集合、装箱、拆箱、字典集合与Hashtable区别
- C#中字典集合HashTable、Dictionary、ConcurrentDictionary三者区别
- C#集合类(HashTable, Dictionary, ArrayList,List)与HashTable线程安全
- [网络收集]C#集合类(HashTable, Dictionary, ArrayList)与HashTable线程安全
- c#中List、Dictionary、ArrayList、Hashtable和数组的区别是什么?
- c#中Dictionary,ArrayList,Hashtable和Array的区别
- c#中Dictionary、ArrayList、Hashtable和数组 Array 的区别(转)
- C# 集合类总结 :(Array、 Arraylist、List、Hashtable、Dictionary、Stack、Queue)
- c# 集合类 Dictionary、ArrayList、Hashtable和数组的区别
- c#中Dictionary、ArrayList、Hashtable和数组 Array 的区别
- c#中List、Dictionary、ArrayList、Hashtable和数组的区别是什么?
- c#中List、Dictionary、ArrayList、Hashtable和数组的区别是什么?
- C# 集合类总结(Array,Arraylist,List,Hashtable,Dictionary,Stack,Queue)
- c#中Dictionary、ArrayList、Hashtable和数组 Array 的区别
- c#中Dictionary、ArrayList、Hashtable和数组的区别是什么?[转]
- C#集合类(HashTable, Dictionary, ArrayList)与HashTable线程安全
- C#集合类(HashTable, Dictionary, ArrayList)与HashTable线程安全
- C#中hashtable 与 dictionary的区别