结果已经写在注释中 static void Main(string[] args) { var concurrentDictionary = new Concurrent
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部分,一次是写入操作,包含带锁写入和不带锁写入,其中每个里面又细分为写入字符串和写入一个类,还有一次是遍历操作,同样包含带锁读和不带锁读,其中也分为读取字符串和读取类。
对于写入操作速度:普通词典 > 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;
}
}
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。如有错误或未考虑完全的地方,望不吝赐教。
--结束END--
本文标题: C#并发容器之ConcurrentDictionary与普通Dictionary带锁性能详解
本文链接: https://lsjlt.com/news/123264.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0