• 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;
    
            }
        }
  • 相关阅读:
    python中的线程(zz)
    Bzoj1014 外星人Prefix
    ABC
    终于明白阿里百度这样的大公司,为什么面试经常拿ThreadLocal考验求职者了
    我去面试没带简历,你让我走人?
    利用Python框架pyxxnet_project实现的网络服务
    我以为我对Mysql索引很了解,直到我遇到了阿里的面试官
    CSS必备知识大全
    致 Python 初学者
    从入门到精通,Java学习路线导航
  • 原文地址:https://www.cnblogs.com/2zhyi/p/3116591.html
Copyright © 2020-2023  润新知