码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2016-09-03 12:08:37      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:

学习参考博客: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三者区别

标签:

原文地址:http://www.cnblogs.com/jiangjing/p/5836480.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!