可能重复: .NET:ArrayList的VS列表
您好,
我在网上搜索着,找我矛盾的答案,至今不清楚的,
哪一个是更快名单,其中,T>或ArrayList的< T>
键,是什么原因
I searched around the web and find my conflicting answers and so far unclear as to,
Which one is faster List<T> or ArrayList<T>
and what is the reason?
我猜名单,其中,T&GT;
要快,但不知道在这种特殊情况下,甚至的ArrayList&LT; T&GT;
也被标记为一个泛型类型。
I am guessing List<T>
should be faster but not sure as in this specific case even ArrayList<T>
also is marked as a generic type.
许多感谢, 玛尼
推荐答案
我做了这个给你。
static void Main(string[] args)
{
Stopwatch sw = new Stopwatch();
Console.WriteLine("Adding a million 32bit integers");
sw.Start();
List<int> listA = new List<int>();
for(int i = 0; i < 1000000; i++)
{
listA.Add(i);
}
sw.Stop();
Console.WriteLine("List<int> took {0} ms", sw.ElapsedMilliseconds);
sw.Reset();
sw.Start();
List<object> listB = new List<object>();
for (int i = 0; i < 1000000; i++)
{
listB.Add(i);
}
sw.Stop();
Console.WriteLine("List<object> took {0} ms", sw.ElapsedMilliseconds);
sw.Reset();
sw.Start();
ArrayList listC = new ArrayList();
for(int i = 0; i < 1000000; i++)
{
listC.Add(i);
}
sw.Stop();
Console.WriteLine("ArrayList took {0} ms", sw.ElapsedMilliseconds);
sw.Reset();
Console.WriteLine("n Inserting 1000 values");
//Gen list of random numbers
Random rand = new Random(12345);
int[] insertlocs = new int[1000];
for (int i = 0; i < insertlocs.Length; i++)
insertlocs[i] = rand.Next(1, 999999);
sw.Start();
for (int i = 0; i < insertlocs.Length; i++)
{
listA.Insert(insertlocs[i], i);
}
sw.Stop();
Console.WriteLine("List<int> took {0} ms", sw.ElapsedMilliseconds);
sw.Reset();
sw.Start();
for (int i = 0; i < insertlocs.Length; i++)
{
listB.Insert(insertlocs[i], i);
}
sw.Stop();
Console.WriteLine("List<object> took {0} ms", sw.ElapsedMilliseconds);
sw.Reset();
sw.Start();
for (int i = 0; i < insertlocs.Length; i++)
{
listC.Insert(insertlocs[i], i);
}
sw.Stop();
Console.WriteLine("ArrayList took {0} ms", sw.ElapsedMilliseconds);
sw.Reset();
Console.ReadKey();
}
在我的补偿,名单,其中,INT&GT;
拿了13毫秒,名单,其中,对象&gt;
了69ms,的ArrayList
40毫秒了。
On my comp, List<int>
took 13ms, List<object>
took 69ms, ArrayList
took 40ms.
如果你拥有它,对于引用类型的ArrayList更快。但是,值类型很显然你应该使用List
So there you have it, for reference types ArrayList is faster. But for value types you should obviously use List
编辑:测试插入性能也一样,名单,其中,INT&GT;
了255毫秒,名单,其中,对象&gt;
了723ms, 的ArrayList
了397ms。 ArrayList中的拳击的是几乎等同于没有列出拳!
Testing insert performance too, List<int>
took 255ms, List<object>
took 723ms, ArrayList
took 397ms. ArrayList with boxing is almost on par with List without boxing!
相关推荐
最新文章