最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#并發(fā)容器之ConcurrentDictionary與普通Dictionary帶鎖性能詳解

 更新時間:2021年04月10日 10:25:21   作者:conquerwave  
這篇文章主要介紹了C#并發(fā)容器之ConcurrentDictionary與普通Dictionary帶鎖性能詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

結(jié)果已經(jīng)寫在注釋中

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
    //對于寫入操作并發(fā)詞典要比普通帶鎖詞典要慢
    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
    //對于讀取操作并發(fā)詞典要比普通帶鎖詞典要快
    //concurrentDictionary采用細(xì)粒度鎖定[fine-grained locking]
    //普通帶鎖dictionary采用粗粒度鎖定[coarse-grained locking]
    //在多核多線程的情況下concurrentDictionary將有更好的性能表現(xiàn)
    sw.Restart(); 
    Console.ReadKey();
} 
const string Item = "Dictionary item";
public static string CurrentItem;

補(bǔ)充:C#中普通字典(Dictionary)、并發(fā)字典(ConcurrentDictionary)、和哈希表(Hashtable)讀寫性能比較

一、說明

程序有時候需要并發(fā)多線程操作,多線程讀取同一個容器內(nèi)的東西是可以的,但是如果需要修改及寫入到同一容器內(nèi),會有索引失敗的問題,即兩個進(jìn)程同時向同一個位置寫入內(nèi)容,這種情況下需要通過lock(var),將容器鎖定,也可以直接使用可并發(fā)讀寫的容器(ConcurrentDictionary)

測試分2部分,一次是寫入操作,包含帶鎖寫入和不帶鎖寫入,其中每個里面又細(xì)分為寫入字符串和寫入一個類,還有一次是遍歷操作,同樣包含帶鎖讀和不帶鎖讀,其中也分為讀取字符串和讀取類。

二、測試結(jié)果

2.1、寫入用時

2.2、遍歷用時

2.3、結(jié)論

對于寫入操作速度:普通詞典 > HashTable > 并發(fā)詞典

對于讀操作速度:并發(fā)字典 > 帶鎖字典 > 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;//執(zhí)行次數(shù) 
            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("向子典寫入【學(xué)生類】不添加鎖(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("向子典寫入【學(xué)生類】添加鎖(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寫入【學(xué)生類】不添加鎖(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寫入【學(xué)生類】添加鎖(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寫入【學(xué)生類】 花費時間為:{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("遍歷【學(xué)生類】字典(無鎖) 花費時間為:{0} 毫秒", _SW.Elapsed.TotalMilliseconds);
 
            //遍歷類字典(有鎖)
            _SW.Restart();
            for (int i = 0; i < _NUM; i++)
            {
                lock (_Dic)
                {
                    _CurrentStudent = _DicClass[i];
                }
            }
            _SW.Stop();
            Console.WriteLine("遍歷【學(xué)生類】字典(有鎖) 花費時間為:{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學(xué)生類】字典(無鎖) 花費時間為:{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學(xué)生類】字典(有鎖) 花費時間為:{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字典】(學(xué)生類) 花費時間為:{0} 毫秒", _SW.Elapsed.TotalMilliseconds);
	    Console.WriteLine("--------------------------------------------------------");
            _SW.Restart(); 
            Console.WriteLine("-------------------結(jié)束---------------------------"); 
            Console.ReadLine();
        }            
    }//Class_end
    public class Student
    {
        public string Name;
        public int Age;
    }
}

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。

您可能感興趣的文章:

相關(guān)文章

最新評論

涞源县| 南岸区| 五莲县| 忻城县| 郎溪县| 长兴县| 满城县| 綦江县| 和田县| 永胜县| 黄骅市| 庆城县| 奉化市| 泗洪县| 香河县| 新建县| 罗甸县| 新乡县| 德江县| 湘阴县| 咸宁市| 平谷区| 阳原县| 金坛市| 临武县| 治多县| 庄河市| 邯郸县| 昭通市| 多伦县| 西城区| 加查县| 彭泽县| 麦盖提县| 屯门区| 乌鲁木齐县| 大英县| 海门市| 乾安县| 琼海市| 星座|