装箱和拆箱的性能损耗以及没有装箱和拆箱的性能损耗

   List<int> listint = new List<int>();
            DateTime start = DateTime.Now;
            for (int i = 0; i < 10000000; i++)
            {
                listint.Add(i);
            }
            DateTime stop = DateTime.Now;
    
            Console.WriteLine("没有装箱和拆箱耗时:"+(stop-start));
            List<string> list1 = new List<string>();
            DateTime start1 = DateTime.Now;
            for (int i = 0; i < 10000000; i++)
            {
                list1.Add(i.ToString());
            }
            DateTime stop1 = DateTime.Now;
            Console.WriteLine("装箱箱耗时:" + (stop1 - start1));

            List<string> list2 = new List<string>();
            DateTime start2 = DateTime.Now;
            for (int i = 0; i < 10000000; i++)
            {
                listint.Add(int.Parse(list1[i]));
            }
            DateTime stop2 = DateTime.Now;
            Console.WriteLine("拆箱箱耗时:" + (stop2 - start2));

下面给你们分享几次测试的结果

猜你喜欢

转载自www.cnblogs.com/zxp6/p/9185118.html