首页 > 代码库 > C#中字典集合HashTable、Dictionary、ConcurrentDictionary三者区别

C#中字典集合HashTable、Dictionary、ConcurrentDictionary三者区别

学习参考博客:http://www.cnblogs.com/yinrq/p/5584885.html

使用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#中字典集合HashTable、Dictionary、ConcurrentDictionary三者区别