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

发布时间:2020-07-18 05:47:08 作者:ly_bing
来源:网络 阅读:1153
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();
}

效果如图:

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

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


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

推荐阅读:
  1. 从零开始-打造自己的虚拟实验室-3
  2. 从零开始-打造自己的虚拟实验室-2

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

性能 list 数组

上一篇:手动启动与开机自动启动网卡的两种方式

下一篇:同时丢失参数文件、控制文件及redo log file的不完全恢复(有数据文件的热备和归档)

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》