zoukankan      html  css  js  c++  java
  • ConcurrentDictionary,ConcurrentStack,ConcurrentQueue

    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;
    

      

  • 相关阅读:
    dubbox编译安装本地maven仓库
    NameNode中几个关键的数据结构
    spark学习总结
    Spark性能优化指南——基础篇
    Spark性能优化指南——高级篇
    Kafka文件存储机制那些事
    Presto实现原理和美团的使用实践
    archlinux locale-gen 命令出错
    git无法连接bitbucket/github时,出现"Permission deied(publickey)"
    转:《JavaScript—之对象参数的引用传递》
  • 原文地址:https://www.cnblogs.com/xiangxiong/p/7661638.html
Copyright © 2011-2022 走看看