• Dictionary 比List占用更多的内存


    通过使用Dictionary和List创建数据, 比较那个占用的内存更多

    class Program
        {
            static void Main(string[] args)
            {
                long start = GC.GetTotalMemory(true);
                //MultData testData=new MultData();
    
    
                //var gcMultListStr = MultData.MultListStr();
                var gcMultDicStr = MultData.MultDicStr();
                //Test1 test = new Test1();
                //Test2 test = new Test2();
                GC.Collect();
                GC.WaitForFullGCComplete();
                long end = GC.GetTotalMemory(true);
                //所消耗的内存
                long useMemory = end - start;
                Console.WriteLine("use memory: " + useMemory);
                Console.Read();
                //4194368,4194356
                //
                //536872908,536872896
                //
                //67110884(List),239981092(Dictionary) Dictionary比List消耗很多性能
            }
        }
    
    public class MultData
        {
            public static List<int> MultListStr()
            {
                //监控创建所用到的时间
                System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
                watch.Start();
                List<int> list = new List<int>();
                for (int i = 0; i < 10000000; i++)
                {
                    list.Add(i);
                }
                watch.Stop();
                //显示创建时间
                var useTime = (double)watch.ElapsedMilliseconds / 1000;
                Console.WriteLine("Use time: " + useTime);
                return list;
    
            }
            public static Dictionary<int, int> MultDicStr()
            {
                //监控创建所用到的时间
                System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
                watch.Start();
                Dictionary<int, int> dic = new Dictionary<int, int>();
            
                for (int i = 0; i < 10000000; i++)
                {
                    dic.Add(i, i);
                }
                watch.Stop();
                //显示创建时间
                var useTime = (double)watch.ElapsedMilliseconds / 1000;
                Console.WriteLine("Use time: " + useTime);
                return dic;
    
            }
        }
  • 相关阅读:
    UVA11039
    UVA10970大块巧克力
    UVA10970大块巧克力
    UVA10340子序列
    UVA10340子序列
    UVA10382喷水装置
    UVA10382喷水装置
    UVA10020(最小区间覆盖)
    洛谷 P2141 珠心算测验
    UVa 11292 勇者斗恶龙(The Dragon of Loowater)
  • 原文地址:https://www.cnblogs.com/2zhyi/p/3116591.html
Copyright © 2020-2023  润新知