ArrayList Array List<T>性能比较
2009-12-01 10:02
471 查看
一直知道ArrayList性能不太好,今天就来试了一下, 贴下来以后使用时做个参考.
请看下面的代码:
代码
using System;
using System.Collections;
using System.Diagnostics;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace Csharp.Test
{
public class Program
{
public static void Main(string[] args)
{
var k = 1500000;
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
ArrayList arrayList = new ArrayList();
for (int i = 0; i < k; i++)
{
arrayList.Add(i);
}
foreach (int Item in arrayList)
{
}
stopWatch.Stop();
Console.WriteLine("ArrayList所花的时间:" + stopWatch.ElapsedMilliseconds);
stopWatch.Reset();
stopWatch.Start();
int[] array = new int[k];
for (int i = 0; i < k; i++)
{
array[i] = i;
}
foreach (int Item in array)
{
}
stopWatch.Stop();
Console.WriteLine("Array所花的时间:" + stopWatch.ElapsedMilliseconds);
stopWatch.Reset();
stopWatch.Start();
List<int> listInt = new List<int>();
for (int i = 0; i < k; i++)
{
listInt.Add(i);
}
foreach (int Item in listInt)
{
}
stopWatch.Stop();
Console.WriteLine("List所花的时间:" + stopWatch.ElapsedMilliseconds);
stopWatch.Reset();
Console.ReadLine();
}
}
}
运行就可以看到,性能的区别的
ArrayList 360
Array 25
List<T> 60
从上面的结果可以看出, 360与25之让的差距. 不同项目不同需求, 小项目用ArrayList 能使工作简单, 用也是可以的, 只是做个测试, 并不是排挤, 毕竟微软还是把它做出来了. 所以建议尽量使用Array, 因为往ArrayList里面添加和修改元素,都会引起装箱和拆箱的操作,频繁的操作可能会影响一部分效率。
请看下面的代码:
代码
using System;
using System.Collections;
using System.Diagnostics;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace Csharp.Test
{
public class Program
{
public static void Main(string[] args)
{
var k = 1500000;
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
ArrayList arrayList = new ArrayList();
for (int i = 0; i < k; i++)
{
arrayList.Add(i);
}
foreach (int Item in arrayList)
{
}
stopWatch.Stop();
Console.WriteLine("ArrayList所花的时间:" + stopWatch.ElapsedMilliseconds);
stopWatch.Reset();
stopWatch.Start();
int[] array = new int[k];
for (int i = 0; i < k; i++)
{
array[i] = i;
}
foreach (int Item in array)
{
}
stopWatch.Stop();
Console.WriteLine("Array所花的时间:" + stopWatch.ElapsedMilliseconds);
stopWatch.Reset();
stopWatch.Start();
List<int> listInt = new List<int>();
for (int i = 0; i < k; i++)
{
listInt.Add(i);
}
foreach (int Item in listInt)
{
}
stopWatch.Stop();
Console.WriteLine("List所花的时间:" + stopWatch.ElapsedMilliseconds);
stopWatch.Reset();
Console.ReadLine();
}
}
}
运行就可以看到,性能的区别的
ArrayList 360
Array 25
List<T> 60
从上面的结果可以看出, 360与25之让的差距. 不同项目不同需求, 小项目用ArrayList 能使工作简单, 用也是可以的, 只是做个测试, 并不是排挤, 毕竟微软还是把它做出来了. 所以建议尽量使用Array, 因为往ArrayList里面添加和修改元素,都会引起装箱和拆箱的操作,频繁的操作可能会影响一部分效率。
相关文章推荐
- Array,ArrayList 和 List<T>的选择和性能比较.
- C# List<>与Array性能比较
- List<T> 与ArrayList 性能比较
- C# List<>与Array性能比较
- C# List<>与Array性能比较
- C# List<>与Array性能比较
- 数组排序方法的性能比较(2):Array.Sort<T>实现分析
- ArrayList的使用和List<T>的比较
- C# 中的集合(Array/ArrayList/List<T>/HashTable/Dictionary)
- 数组、LIst<> 、 ArrayList的性能对比
- c#初学-泛型ArrayList的使用和List<T>的比较
- C#中Array与ArrayList与泛型数组 list<>的区别
- C# 中的集合(Array/ArrayList/List<T>/HashTable/Dictionary)
- Android SparseArray和HashMap 性能比较<3>
- List<T>在搜索和排序时采用不同方法的性能比较
- 粗谈C#里 dictionary,HashTable,List<T>,Array 的性能优缺和实现原理。
- .net集合类的研究--Array,ArrayList,List<T>
- C#中的[],Array,List<>,ArrayList的区别
- C# 中的集合(Array/ArrayList/List<T>/HashTable/Dictionary)
- 艾伟_转载:数组排序方法的性能比较(中):Array.Sort<T> 实现分析