装箱和拆箱的区别

装箱:把值类型转化为应用类型

拆箱:把应用类型转化为值类型:

ex:int a=10; string sa=a;//装箱
int a1=Int32.Parse(sa);//拆箱

性能损失:

相比于简单的赋值操作,装箱和拆箱需要进行大量的计算,对值类型进行装箱时,需要分配并构造一个全新的对象。

为了解决装箱与拆箱带来的性能损失问题,微软公司在.NET中提供了泛型。

下面就让我们看一段代码:

将5000000条记录写入到ArrayList并将数据读出来5遍,其中写入和读取ArrayList的工作就是装箱和拆箱,最后计算出执行这些方法所消耗的时间。

class Unbox

    {

        public static void RunUnbox()

        {

            int count;

            DateTime startTime = DateTime.Now;

            ArrayList MyArrayList = new ArrayList();

            for (int i = 0; i < 5; i++)

            {

                MyArrayList.Clear();

                for (count = 0; count < 5000000; count++)

                {

                    MyArrayList.Add(count); //装箱

                }

                int j;

                for (count = 0; count < 5000000; count++)

                {

                    j = (int)MyArrayList[count];//拆箱

                }

            }

            DateTime endTime = DateTime.Now;

            Console.WriteLine("使用装箱和拆箱共花时间:{0}",endTime-startTime);

        }

        public static void RunNoBox()

        {

            int count;

            DateTime startTime = DateTime.Now;

            List<int> MyTList = new List<int>();

            for (int i = 0; i < 5; i++)

            {

                MyTList.Clear();

                for (count = 0; count < 5000000; count++)

                {

                    MyTList.Add(count);

                }

                int j;

                for (count = 0; count < 5000000; count++)

                {

                    j = MyTList[count];

                }

            }

            DateTime endTime = DateTime.Now;

            Console.WriteLine("使用泛型共花时间:{0}", endTime - startTime);      

        }

    }

    class Test

    {

        static void Main(string[] args)

        {

            Unbox.RunUnbox();

            Unbox.RunNoBox();

            Console.ReadLine();

        }

    }

下面是运行结果:

使用装箱和拆箱共花时间:00:00:04.2500000

使用泛型共花时间:00:00:00.4687500

由运行结果可以发现,即使是进行同样的操作,使用装箱和拆箱所使用的时间是使用泛型所花时间的10倍。

部分转自:http://www.cnblogs.com/guowenhui/archive/2011/10/16/2213803.html

posted @ 2013-03-17 17:49  四条眉毛  阅读(663)  评论(0编辑  收藏  举报