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());获得线程安全的对象)

转载于:https://www.cnblogs.com/jiangjing/p/5836480.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值