首页 > 编程语言 > 详细

数组、LIst<> 、 ArrayList的性能对比

时间:2016-06-08 06:57:56      阅读:230      评论:0      收藏:0      [点我收藏+]
static void Main(string[] args)
{
    Stopwatch sw = new Stopwatch();

    int[] intArray = new int[100];
    sw.Start();
    for (int i = 0; i < 100; i++)
    {
        intArray[i] = i;
    }
    sw.Stop();
    Console.WriteLine(" Add 0 ~ 100 to int[100] : " + sw.Elapsed);

    ArrayList list = new ArrayList();
    sw = new Stopwatch();
    sw.Start();
    for (int i = 0; i < 100; i++)
    {
        list.Add(i);
    }
    sw.Stop();
    Console.WriteLine(" Add 0 ~ 100 to ArrayList : " + sw.Elapsed);

    List<int> intList = new List<int>();
    sw = new Stopwatch();
    sw.Start();
    for (int i = 0; i < 100; i++)
    {
        intList.Add(i);
    }
    sw.Stop();
    Console.WriteLine(" Add 0 ~ 100 to List<int> : " + sw.Elapsed);

    Console.ReadLine();
}

效果如图:

技术分享

可以看到数组明显比较快,但是必需初始化长度


目测原因是往ArrayList中添加元素时发生了装箱操作

本文出自 “lybing” 博客,请务必保留此出处http://lybing.blog.51cto.com/3286625/1787125

数组、LIst<> 、 ArrayList的性能对比

原文:http://lybing.blog.51cto.com/3286625/1787125

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!