数组与列表的性能 [英] Performance of Arrays vs. Lists

查看:37
本文介绍了数组与列表的性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您需要一个需要经常迭代的整数列表/数组,我的意思是非常频繁.原因可能各不相同,但可以说它位于高容量处理的最内层循环的核心.

Say you need to have a list/array of integers which you need iterate frequently, and I mean extremely often. The reasons may vary, but say it's in the heart of the inner most loop of a high volume processing.

一般来说,人们会选择使用列表(List),因为它们在大小上很灵活.最重要的是,msdn 文档声称列表在内部使用数组并且应该执行同样快(使用 Reflector 快速查看证实了这一点).然而,这涉及到一些开销.

In general, one would opt for using Lists (List) due to their flexibility in size. On top of that, msdn documentation claims Lists use an array internally and should perform just as fast (a quick look with Reflector confirms this). Neverless, there is some overhead involved.

有人实际测量过吗?遍历一个列表 6M 次的时间会和数组一样吗?

Did anyone actually measure this? would iterating 6M times through a list take the same time as an array would?

推荐答案

非常容易衡量...

在少量的紧循环处理代码中我知道长度是固定的我使用数组来进行额外的微优化;数组可以略微更快如果您使用索引器/for form - 但 IIRC 认为这取决于数组中的数据类型.但除非您需要微优化,否则请保持简单并使用List

In a small number of tight-loop processing code where I know the length is fixed I use arrays for that extra tiny bit of micro-optimisation; arrays can be marginally faster if you use the indexer / for form - but IIRC believe it depends on the type of data in the array. But unless you need to micro-optimise, keep it simple and use List<T> etc.

当然,这仅适用于您正在阅读所有数据的情况;对于基于键的查找,字典会更快.

Of course, this only applies if you are reading all of the data; a dictionary would be quicker for key-based lookups.

这是我使用int"的结果(第二个数字是校验和,用于验证它们都做了相同的工作):

Here's my results using "int" (the second number is a checksum to verify they all did the same work):

(已编辑以修复错误)

List/for: 1971ms (589725196)
Array/for: 1864ms (589725196)
List/foreach: 3054ms (589725196)
Array/foreach: 1860ms (589725196)

基于测试台:

using System;
using System.Collections.Generic;
using System.Diagnostics;
static class Program
{
    static void Main()
    {
        List<int> list = new List<int>(6000000);
        Random rand = new Random(12345);
        for (int i = 0; i < 6000000; i++)
        {
            list.Add(rand.Next(5000));
        }
        int[] arr = list.ToArray();

        int chk = 0;
        Stopwatch watch = Stopwatch.StartNew();
        for (int rpt = 0; rpt < 100; rpt++)
        {
            int len = list.Count;
            for (int i = 0; i < len; i++)
            {
                chk += list[i];
            }
        }
        watch.Stop();
        Console.WriteLine("List/for: {0}ms ({1})", watch.ElapsedMilliseconds, chk);

        chk = 0;
        watch = Stopwatch.StartNew();
        for (int rpt = 0; rpt < 100; rpt++)
        {
            for (int i = 0; i < arr.Length; i++)
            {
                chk += arr[i];
            }
        }
        watch.Stop();
        Console.WriteLine("Array/for: {0}ms ({1})", watch.ElapsedMilliseconds, chk);

        chk = 0;
        watch = Stopwatch.StartNew();
        for (int rpt = 0; rpt < 100; rpt++)
        {
            foreach (int i in list)
            {
                chk += i;
            }
        }
        watch.Stop();
        Console.WriteLine("List/foreach: {0}ms ({1})", watch.ElapsedMilliseconds, chk);

        chk = 0;
        watch = Stopwatch.StartNew();
        for (int rpt = 0; rpt < 100; rpt++)
        {
            foreach (int i in arr)
            {
                chk += i;
            }
        }
        watch.Stop();
        Console.WriteLine("Array/foreach: {0}ms ({1})", watch.ElapsedMilliseconds, chk);

        Console.ReadLine();
    }
}

这篇关于数组与列表的性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆