码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2016-06-08 07:00:09      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:list<t>   性能   数组   

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的性能对比

标签:list<t>   性能   数组   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!