zoukankan      html  css  js  c++  java
  • 基础才是重中之重~Dictionary<K,V>里V的设计决定的性能

    回到目录

    字典对象Dictionary<K,V>我们经常会用到,而在大数据环境下,字典使用不当可能引起性能问题,严重的可能引起内在的溢出!

    • 字典的值建议为简单类型,反正使用Tuple<T>
    • 字典的键在查找时,时间复杂度为O(1),性能不会有任何问题,所以不要愿望它

    下面代码是对500万的字典进行测试,首先赋值,然后取出一个随机机,性能在毫秒级

        static void Draw()
            {
                int count = 5000000;
                Console.WriteLine("test:{0} feeds", count);
    
                List<GoldCoinInfo> list = new List<GoldCoinInfo>();
                list.Add(new GoldCoinInfo { Id = 100, GoldValue = 5, LeftCount = count, TotalCount = count });
                var dic = new Dictionary<int, int>();
                int _index = 0;
                Stopwatch sw = new Stopwatch();
                sw.Restart();
                foreach (var gold in list)
                {
                    for (int j = 0; j < gold.LeftCount; j++)
                    {
                        dic.Add(_index, gold.Id);
                        _index++;
                    }
                }
                sw.Stop();
                Console.WriteLine("step1:{0} ms", sw.ElapsedMilliseconds);
                sw.Restart();
                var prizeIndex2 = GenerateRandom(dic.Keys.Max(), 1).FirstOrDefault();
                Console.WriteLine("step3:{0} ms,value:{1}", sw.ElapsedMilliseconds, dic[prizeIndex2]);
                sw.Stop();
            }

    测试结果

    而如果value使用了tuple<t>类型,那性能就一落千丈了!

           var dic = new Dictionary<int, Tuple<int, int>>();
                int _index = 0;
                Stopwatch sw = new Stopwatch();
                sw.Restart();
                foreach (var gold in list)
                {
                    for (int j = 0; j < gold.LeftCount; j++)
                    {
                        dic.Add(_index, new Tuple<int, int>(gold.Id, gold.GoldValue));
                        _index++;
                    }
                }

    在取随机机时,我们有时使用NewId()这试,但这种开销依然很大,不建议大家使用,这种只适合在特定的场合用,如EF对IQueryable结果集动态随机数时,代码如下

       /// <summary>
        /// sql函数的扩展类
        /// </summary>
        public static class SqlFunctionExtensions
        {
    
            #region 功能方法
            /// <summary>
            /// 在linq to entity中使用SqlServer.NEWID函数
            /// </summary>
            public static Guid NewId()
            {
                return Guid.NewGuid();
            }
            #endregion
    
            #region 扩展方法
            /// <summary>
            /// 随机排序扩展方法
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="source"></param>
            /// <returns></returns>
            public static IQueryable<T> OrderByNewId<T>(this IEnumerable<T> source)
            {
                return source.AsQueryable().OrderBy(d => NewId());
            }
            #endregion
    
        }

    对技术的研究我们在继续,有时,模棱两可是不行的!

    有时,应该较较真!

    回到目录

  • 相关阅读:
    WPF学习10:基于MVVM Light 制作图形编辑工具(1)
    外文翻译 《How we decide》赛场上的四分卫
    算法学习01:二分查询,选择法、插入法、分治法排序
    外文翻译 《How we decide》 Introduction
    WPF学习09:数据绑定之 Binding to List Data
    WPF学习08:MVVM 预备知识之COMMAND
    WPF学习07:MVVM 预备知识之数据绑定
    WPF学习05:2D绘图 使用Transform进行控件变形
    WPF学习06:转换控件内容为可存储图片
    WPF学习04:2D绘图 使用Shape绘基本图形
  • 原文地址:https://www.cnblogs.com/lori/p/6693865.html
Copyright © 2011-2022 走看看