C#并发容器之ConcurrentDictionary与普通Dictionary带锁性能详解

 更新时间:2021年04月10日 10:25:21   作者:conquerwave  
这篇文章主要介绍了C#并发容器之ConcurrentDictionary与普通Dictionary带锁性能详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

结果已经写在注释中

static void Main(string[] args)
{
    var concurrentDictionary = new ConcurrentDictionary<int, string>();
    var dictionary = new Dictionary<int, string>(); 
    var sw = new Stopwatch();
    sw.Start();
 
    for (int i = 0; i < 1000000; i++)
    {
        lock (dictionary)
        {
            dictionary[i] = Item;
        }
    } 
    sw.Stop();
    Console.WriteLine("wrinting to dictionary with a lock: {0}", sw.Elapsed);
    //wrinting to dictionary with a lock: 00:00:00.0633939
    sw.Restart();
    for (int i = 0; i < 1000000; i++)
    {
        concurrentDictionary[i] = Item;
    }
    sw.Stop();
    Console.WriteLine("wrinting to a concurrent dictionary: {0}", sw.Elapsed);
    //wrinting to a concurrent dictionary: 00:00:00.2889851
    //对于写入操作并发词典要比普通带锁词典要慢
    sw.Restart();
    for (int i = 0; i < 1000000; i++)
    {
        lock (dictionary)
        {
            CurrentItem = dictionary[i];
        }
    }
    sw.Stop();
    Console.WriteLine("reading from dictionary with a lock: {0}", sw.Elapsed);
    //reading from dictionary with a lock: 00:00:00.0286066
    sw.Restart();
    for (int i = 0; i < 1000000; i++)
    {
        CurrentItem = concurrentDictionary[i];
    }
    sw.Stop();
    Console.WriteLine("reading from a concurrent dictionary: {0}", sw.Elapsed);
    //reading from a concurrent dictionary: 00:00:00.0196372
    //对于读取操作并发词典要比普通带锁词典要快
    //concurrentDictionary采用细粒度锁定[fine-grained locking]
    //普通带锁dictionary采用粗粒度锁定[coarse-grained locking]
    //在多核多线程的情况下concurrentDictionary将有更好的性能表现
    sw.Restart(); 
    Console.ReadKey();
} 
const string Item = "Dictionary item";
public static string CurrentItem;

补充:C#中普通字典(Dictionary)、并发字典(ConcurrentDictionary)、和哈希表(Hashtable)读写性能比较

一、说明

程序有时候需要并发多线程操作,多线程读取同一个容器内的东西是可以的,但是如果需要修改及写入到同一容器内,会有索引失败的问题,即两个进程同时向同一个位置写入内容,这种情况下需要通过lock(var),将容器锁定,也可以直接使用可并发读写的容器(ConcurrentDictionary)

测试分2部分,一次是写入操作,包含带锁写入和不带锁写入,其中每个里面又细分为写入字符串和写入一个类,还有一次是遍历操作,同样包含带锁读和不带锁读,其中也分为读取字符串和读取类。

二、测试结果

2.1、写入用时

2.2、遍历用时

2.3、结论

对于写入操作速度:普通词典 > HashTable > 并发词典

对于读操作速度:并发字典 > 带锁字典 > HashTable

无论普通字典还是HashTable,带锁花费的时间都要比不带锁慢,为了线程安全,肯定要牺牲时间的。

所以如果需要自己写入的话,推荐带锁普通字典,读写速度都很均衡。

三、测试代码如下

using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks; 
namespace BaseMultiThread
{
    class Program
    {      
        static void Main(string[] args)
        {
            ConcurrentDictionary<int, string> _CctDic= new ConcurrentDictionary<int, string>();
            ConcurrentDictionary<int, Student> _CctDicClass = new ConcurrentDictionary<int, Student>(); 
            Dictionary<int, string> _Dic = new Dictionary<int, string>();
            Dictionary<int, Student> _DicClass = new Dictionary<int, Student>(); 
            Hashtable _Ht = new Hashtable();
            Hashtable _HtClass = new Hashtable(); 
            string _CurrentItem = "";
            const string _Item = "字符串";
            const int _NUM = 10000000;//执行次数 
            Student _CurrentStudent = null;
            Student student = new Student { Name = _Item, Age = 23 }; 
            Stopwatch _SW = new Stopwatch();
 
            //字符串写入字典(无锁)
            _SW.Start();
 
            for (int i = 0; i < _NUM; i++)
            {
                _Dic[i] = _Item;
            }
            _SW.Stop();
            Console.WriteLine("向字典写入【字符串】不添加锁(Lock)花费时间为:{0} 毫秒", _SW.Elapsed.TotalMilliseconds);
 
            //字符串写入字典(有锁)
            _Dic = new Dictionary<int, string>();
            _SW.Restart();
            for (int i = 0; i < _NUM; i++)
            {
                lock (_Dic)
                {
                    _Dic[i] = _Item;
                }  
            }
            _SW.Stop();
            Console.WriteLine("向字典写入【字符串】添加锁(Lock)花费时间为:{0} 毫秒", _SW.Elapsed.TotalMilliseconds);
 
            //类写入字典(无锁)
            _SW.Restart();
            for (int i = 0; i < _NUM; i++)
            {
                _DicClass[i] = student;
            }
            _SW.Stop();
            Console.WriteLine("向子典写入【学生类】不添加锁(Lock)花费时间为:{0} 毫秒", _SW.Elapsed.TotalMilliseconds);
 
            //类写入字典(有锁)
            _DicClass = new Dictionary<int, Student>();
            _SW.Restart();
            for (int i = 0; i < _NUM; i++)
            {
                lock (_DicClass)
                {
                    _DicClass[i] = student;
                }
            }
            _SW.Stop();
            Console.WriteLine("向子典写入【学生类】添加锁(Lock)花费时间为:{0} 毫秒", _SW.Elapsed.TotalMilliseconds);
            Console.WriteLine("----------------------------------------------------");
 
            //字符串写入HashTable(无锁)
            _SW.Restart();
            for (int i = 0; i < _NUM; i++)
            {
                _Ht[i] = _Item;
            }
            _SW.Stop();
            Console.WriteLine("向HashTable写入【字符串】不添加锁(Lock)花费时间为:{0} 毫秒", _SW.Elapsed.TotalMilliseconds);
 
            //字符串写入HashTable(有锁)
            _Ht = new Hashtable();
            _SW.Restart();
            for (int i = 0; i < _NUM; i++)
            {
                lock (_Ht)
                {
                    _Ht[i] = _Item;
                }
            }
            _SW.Stop();
            Console.WriteLine("向HashTable写入【字符串】添加锁(Lock)花费时间为:{0} 毫秒", _SW.Elapsed.TotalMilliseconds);
 
            //类写入HashTable(无锁)
            _SW.Restart();
            for (int i = 0; i < _NUM; i++)
            {
                _HtClass[i] = student;
            }
            _SW.Stop();
            Console.WriteLine("向HashTable写入【学生类】不添加锁(Lock)花费时间为:{0} 毫秒", _SW.Elapsed.TotalMilliseconds);
 
            //类写入HashTable(有锁)
            _SW.Restart();
            for (int i = 0; i < _NUM; i++)
            {
                lock (_HtClass)
                {
                    _HtClass[i] = student;
                }               
            }
            _SW.Stop();
            Console.WriteLine("向HashTable写入【学生类】添加锁(Lock)花费时间为:{0} 毫秒", _SW.Elapsed.TotalMilliseconds);
            Console.WriteLine("----------------------------------------------------------");
 
            //字符串写入ConcurrentDictionary
            _SW.Restart();
            for (int i = 0; i < _NUM; i++)
            {
                _CctDic[i] = _Item;
            }
            _SW.Stop();
            Console.WriteLine("向ConcurrentDictionary写入【字符串】 花费时间为:{0} 毫秒", _SW.Elapsed.TotalMilliseconds);
 
            //类写入ConcurrentDictionary
            _SW.Restart();
            for (int i = 0; i < _NUM; i++)
            {
                _CctDicClass[i] = student;
            }
            _SW.Stop();
            Console.WriteLine("向ConcurrentDictionary写入【学生类】 花费时间为:{0} 毫秒", _SW.Elapsed.TotalMilliseconds);
            Console.WriteLine("--------------------------------------------------------");
 
            //遍历普通字典(无锁)
            _SW.Restart();
            for (int i = 0; i < _NUM; i++)
            {
                _CurrentItem = _Dic[i];
            }
            _SW.Stop();
            Console.WriteLine("遍历【普通】字典(无锁) 花费时间为:{0} 毫秒", _SW.Elapsed.TotalMilliseconds);
 
            //遍历普通字典(有锁)
            _SW.Restart();
            for (int i = 0; i < _NUM; i++)
            {
                lock (_Dic)
                {
                    _CurrentItem = _Dic[i];
                }
            }
            _SW.Stop();
            Console.WriteLine("遍历【普通】字典(有锁) 花费时间为:{0} 毫秒", _SW.Elapsed.TotalMilliseconds);
 
            //遍历类字典(无锁)
            _SW.Restart();
            for (int i = 0; i < _NUM; i++)
            {
                _CurrentStudent = _DicClass[i];
            }
            _SW.Stop();
            Console.WriteLine("遍历【学生类】字典(无锁) 花费时间为:{0} 毫秒", _SW.Elapsed.TotalMilliseconds);
 
            //遍历类字典(有锁)
            _SW.Restart();
            for (int i = 0; i < _NUM; i++)
            {
                lock (_Dic)
                {
                    _CurrentStudent = _DicClass[i];
                }
            }
            _SW.Stop();
            Console.WriteLine("遍历【学生类】字典(有锁) 花费时间为:{0} 毫秒", _SW.Elapsed.TotalMilliseconds);
            Console.WriteLine("--------------------------------------------------------");
 
            //遍历HashTable(无锁)
            _SW.Restart();
            for (int i = 0; i < _NUM; i++)
            {
                _CurrentItem = _Ht[i].ToString();
            }
            _SW.Stop();
            Console.WriteLine("遍历【HashTable】字典(无锁) 花费时间为:{0} 毫秒", _SW.Elapsed.TotalMilliseconds);
 
            //遍历HashTable(有锁)
            _SW.Restart();
            for (int i = 0; i < _NUM; i++)
            {
                lock (_Dic)
                {
                    _CurrentItem = _Ht[i].ToString();
                }
            }
            _SW.Stop();
            Console.WriteLine("遍历【HashTable】字典(有锁) 花费时间为:{0} 毫秒", _SW.Elapsed.TotalMilliseconds);
 
            //遍历HashTable类(无锁)
            _SW.Restart();
            for (int i = 0; i < _NUM; i++)
            {
                _CurrentStudent = (Student)_HtClass[i];
            }
            _SW.Stop();
            Console.WriteLine("遍历【HashTable学生类】字典(无锁) 花费时间为:{0} 毫秒", _SW.Elapsed.TotalMilliseconds);
 
            //遍历HashTable类(有锁)
            _SW.Restart();
            for (int i = 0; i < _NUM; i++)
            {
                lock (_Dic)
                {
                    _CurrentStudent = (Student)_HtClass[i];
                }
            }
            _SW.Stop();
            Console.WriteLine("遍历【HashTable学生类】字典(有锁) 花费时间为:{0} 毫秒", _SW.Elapsed.TotalMilliseconds);
            Console.WriteLine("--------------------------------------------------------");
 
            //遍历ConCurrent字典
            _SW.Restart();
            for (int i = 0; i < _NUM; i++)
            {
                _CurrentItem = _CctDic[i];
            }
            _SW.Stop();
            Console.WriteLine("遍历【ConCurrent字典】(字符串) 花费时间为:{0} 毫秒", _SW.Elapsed.TotalMilliseconds);
 
            //遍历ConCurrent字典(类)
            _SW.Restart();
            for (int i = 0; i < _NUM; i++)
            {
                _CurrentStudent = _CctDicClass[i];
            }
            _SW.Stop();
            Console.WriteLine("遍历【ConCurrent字典】(学生类) 花费时间为:{0} 毫秒", _SW.Elapsed.TotalMilliseconds);
	    Console.WriteLine("--------------------------------------------------------");
            _SW.Restart(); 
            Console.WriteLine("-------------------结束---------------------------"); 
            Console.ReadLine();
        }            
    }//Class_end
    public class Student
    {
        public string Name;
        public int Age;
    }
}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。如有错误或未考虑完全的地方,望不吝赐教。

您可能感兴趣的文章:

相关文章

  • C#实现单词本功能

    C#实现单词本功能

    这篇文章主要为大家详细介绍了C#实现单词本功能,复习巩固所学单词,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-07-07
  • C#中调整图像大小的步骤详解

    C#中调整图像大小的步骤详解

    这篇文章主要介绍了C#中调整图像大小的步骤详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-05-05
  • c# 字符串操作总结

    c# 字符串操作总结

    这篇文章主要介绍了c# 字符串操作的相关知识,文中讲解的非常详细,代码帮助大家更好的学习,感兴趣的朋友可以参考下
    2020-06-06
  • WPF实现监听快捷键的方式分享

    WPF实现监听快捷键的方式分享

    这篇文章主要为大家详细介绍了WPF实现监听快捷键的几种方式,文中的示例代码讲解详细,具有一定的借鉴与学习价值,需要的可以了解一下
    2023-03-03
  • 深入解析C#中的abstract抽象类

    深入解析C#中的abstract抽象类

    这篇文章主要介绍了深入解析C#中的abstract抽象类,包括定义抽象类等C#面相对象编程中的基础知识,需要的朋友可以参考下
    2016-01-01
  • C#使用正则表达式隐藏手机号中间四位为*

    C#使用正则表达式隐藏手机号中间四位为*

    这篇文章主要介绍了C#使用正则表达式隐藏手机号中间四位为*的相关资料,需要的朋友可以参考下
    2017-06-06
  • 使用Visual Studio2019创建C#项目(窗体应用程序、控制台应用程序、Web应用程序)

    使用Visual Studio2019创建C#项目(窗体应用程序、控制台应用程序、Web应用程序)

    这篇文章主要介绍了使用Visual Studio2019创建C#项目(窗体应用程序、控制台应用程序、Web应用程序),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2020-03-03
  • c#判断操作系统位数的示例分享

    c#判断操作系统位数的示例分享

    这篇文章主要介绍了c#判断操作系统位数的示例,在.net 4.5中,判断操作系统不用再写方法去判断了,有直接可以利用的属性了哦
    2014-01-01
  • 基于C#编写一个合并多个Word文档的工具

    基于C#编写一个合并多个Word文档的工具

    这篇文章主要为大家详细介绍了如何使用C#编写一个小工具,可以实现把多个word文档进行合并成一个word文档,感兴趣的小伙伴可以了解下
    2024-02-02
  • c# WPF如何实现滚动显示的TextBlock

    c# WPF如何实现滚动显示的TextBlock

    这篇文章主要介绍了c# WPF如何实现滚动显示的TextBlock,帮助大家更好的理解和学习使用c#,感兴趣的朋友可以了解下
    2021-03-03

最新评论