C# List<>与Array性能比较
2012-03-28 15:13
337 查看
C# List<>与Array性能比较
在.net framework 2.0之前,数组表示可以用Array 和集合类 ArrayList 表示,2.0后,.net 引进了泛型的概念List<>,那么我们的选择就多了一种。
2.0引进泛型其本意是减少类型的装箱拆箱所带来的性能消耗。
比如;
private void CompareTo<T>(List<T> list)
{
....
}
这里的List<T>就是泛型,调用时我不知道要传入什么类型,有可能是int,string或class.
今天主要讨论的是在类型确定的情况下,哪种性能高一些,ArrayList就不用考虑了,它肯定是最慢的,它只能add object type的.这里主要讨论List<>和Array的性能:
先看string的情况:
static void Main(string[] args)
{
const int COUNT=1000;
string[] array = new string[COUNT];
List<string> list = new List<string>();
Console.WriteLine("the count is:{0}",COUNT);
Console.Write("the value of Total for array:");
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
for (int i = 0; i < COUNT; i++)
{
array[i] = i.ToString(); ;
}
int theArrayTotal = 0;
for (int i = 0; i < COUNT; i++)
{
//theArrayTotal += array[i];
}
stopWatch.Stop();
Console.Write(theArrayTotal);
Console.WriteLine();
Console.Write("array init time:");
Console.Write(stopWatch.ElapsedMilliseconds.ToString());
stopWatch.Reset();
stopWatch.Start();
Console.WriteLine();
for (int i = 0; i < COUNT; i++)
{
list.Add(i.ToString()) ;
}
int theListTotal = 0;
foreach (string v in list)
{
//theListTotal += v;
}
stopWatch.Stop();
Console.WriteLine("the value of Total for list:{0}", theListTotal);
Console.Write("list init time:");
Console.Write(stopWatch.ElapsedMilliseconds.ToString());
stopWatch.Reset();
Console.Read();
}
当COUNT=1000时,两者看不出差别,用的time都是0
当COUNT=10000时,也没什么区别
当COUNT=100000时,
the count is:100000
the value of Total for array:0
array init time:16
the value of Total for list:0
list init time:21
Aarry 比List快了5
当COUNT=1000000时
the count is:1000000
the value of Total for array:0
array init time:296
the value of Total for list:0
list init time:320
Arry比List快了24
当string时,Arry是要比List快
当用int类时:
static void Main(string[] args)
{
const int COUNT=100000;
int[] array = new int[COUNT];
List<int> list = new List<int>();
Console.WriteLine("the count is:{0}",COUNT);
Console.Write("the value of Total for array:");
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
for (int i = 0; i < COUNT; i++)
{
array[i] = i;
}
int theArrayTotal = 0;
for (int i = 0; i < COUNT; i++)
{
//theArrayTotal += array[i];
}
stopWatch.Stop();
Console.Write(theArrayTotal);
Console.WriteLine();
Console.Write("array init time:");
Console.Write(stopWatch.ElapsedMilliseconds.ToString());
stopWatch.Reset();
stopWatch.Start();
Console.WriteLine();
for (int i = 0; i < COUNT; i++)
{
list.Add(i) ;
}
int theListTotal = 0;
foreach (int v in list)
{
//theListTotal += v;
}
stopWatch.Stop();
Console.WriteLine("the value of Total for list:{0}", theListTotal);
Console.Write("list init time:");
Console.Write(stopWatch.ElapsedMilliseconds.ToString());
stopWatch.Reset();
Console.Read();
}
修改COUNT=1000,10000,100000,1000000依次输入为:
the count is:1000
the value of Total for array:0
array init time:0
the value of Total for list:0
list init time:0
the count is:10000
the value of Total for array:0
array init time:0
the value of Total for list:0
list init time:0
the count is:100000
the value of Total for array:0
array init time:0
the value of Total for list:0
list init time:1
the count is:1000000
the value of Total for array:0
array init time:7
the value of Total for list:0
list init time:17
the count is:10000000
the value of Total for array:0
array init time
4000
:77
the value of Total for list:0
list init time:218
(不同的硬件配置会有不同的结果)
从上面的结果来看array的效率要比List的要高一些,当数组长度不是很大时,两者没什么区别,建议用List<>,毕竟是可变长度,可以Add;特殊应用还是建议用array,
不提倡用ArrayList.
引用自cnblogs的文章。
在.net framework 2.0之前,数组表示可以用Array 和集合类 ArrayList 表示,2.0后,.net 引进了泛型的概念List<>,那么我们的选择就多了一种。
2.0引进泛型其本意是减少类型的装箱拆箱所带来的性能消耗。
比如;
private void CompareTo<T>(List<T> list)
{
....
}
这里的List<T>就是泛型,调用时我不知道要传入什么类型,有可能是int,string或class.
今天主要讨论的是在类型确定的情况下,哪种性能高一些,ArrayList就不用考虑了,它肯定是最慢的,它只能add object type的.这里主要讨论List<>和Array的性能:
先看string的情况:
static void Main(string[] args)
{
const int COUNT=1000;
string[] array = new string[COUNT];
List<string> list = new List<string>();
Console.WriteLine("the count is:{0}",COUNT);
Console.Write("the value of Total for array:");
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
for (int i = 0; i < COUNT; i++)
{
array[i] = i.ToString(); ;
}
int theArrayTotal = 0;
for (int i = 0; i < COUNT; i++)
{
//theArrayTotal += array[i];
}
stopWatch.Stop();
Console.Write(theArrayTotal);
Console.WriteLine();
Console.Write("array init time:");
Console.Write(stopWatch.ElapsedMilliseconds.ToString());
stopWatch.Reset();
stopWatch.Start();
Console.WriteLine();
for (int i = 0; i < COUNT; i++)
{
list.Add(i.ToString()) ;
}
int theListTotal = 0;
foreach (string v in list)
{
//theListTotal += v;
}
stopWatch.Stop();
Console.WriteLine("the value of Total for list:{0}", theListTotal);
Console.Write("list init time:");
Console.Write(stopWatch.ElapsedMilliseconds.ToString());
stopWatch.Reset();
Console.Read();
}
当COUNT=1000时,两者看不出差别,用的time都是0
当COUNT=10000时,也没什么区别
当COUNT=100000时,
the count is:100000
the value of Total for array:0
array init time:16
the value of Total for list:0
list init time:21
Aarry 比List快了5
当COUNT=1000000时
the count is:1000000
the value of Total for array:0
array init time:296
the value of Total for list:0
list init time:320
Arry比List快了24
当string时,Arry是要比List快
当用int类时:
static void Main(string[] args)
{
const int COUNT=100000;
int[] array = new int[COUNT];
List<int> list = new List<int>();
Console.WriteLine("the count is:{0}",COUNT);
Console.Write("the value of Total for array:");
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
for (int i = 0; i < COUNT; i++)
{
array[i] = i;
}
int theArrayTotal = 0;
for (int i = 0; i < COUNT; i++)
{
//theArrayTotal += array[i];
}
stopWatch.Stop();
Console.Write(theArrayTotal);
Console.WriteLine();
Console.Write("array init time:");
Console.Write(stopWatch.ElapsedMilliseconds.ToString());
stopWatch.Reset();
stopWatch.Start();
Console.WriteLine();
for (int i = 0; i < COUNT; i++)
{
list.Add(i) ;
}
int theListTotal = 0;
foreach (int v in list)
{
//theListTotal += v;
}
stopWatch.Stop();
Console.WriteLine("the value of Total for list:{0}", theListTotal);
Console.Write("list init time:");
Console.Write(stopWatch.ElapsedMilliseconds.ToString());
stopWatch.Reset();
Console.Read();
}
修改COUNT=1000,10000,100000,1000000依次输入为:
the count is:1000
the value of Total for array:0
array init time:0
the value of Total for list:0
list init time:0
the count is:10000
the value of Total for array:0
array init time:0
the value of Total for list:0
list init time:0
the count is:100000
the value of Total for array:0
array init time:0
the value of Total for list:0
list init time:1
the count is:1000000
the value of Total for array:0
array init time:7
the value of Total for list:0
list init time:17
the count is:10000000
the value of Total for array:0
array init time
4000
:77
the value of Total for list:0
list init time:218
(不同的硬件配置会有不同的结果)
从上面的结果来看array的效率要比List的要高一些,当数组长度不是很大时,两者没什么区别,建议用List<>,毕竟是可变长度,可以Add;特殊应用还是建议用array,
不提倡用ArrayList.
引用自cnblogs的文章。
相关文章推荐
- C# List<>与Array性能比较
- C# List<>与Array性能比较
- C# List<>与Array性能比较
- ArrayList Array List<T>性能比较
- 粗谈C#里 dictionary,HashTable,List<T>,Array 的性能优缺和实现原理。
- Array,ArrayList 和 List<T>的选择和性能比较.
- C#中Array与ArrayList与泛型数组 list<>的区别
- List<T>在搜索和排序时采用不同方法的性能比较
- List<T> 与ArrayList 性能比较
- C#中list<T>性能内存优化分配,Capacity设置
- List<T> 和 Collection<T> 性能比较
- 艾伟_转载:数组排序方法的性能比较(中):Array.Sort<T> 实现分析
- C# 中的集合(Array/ArrayList/List<T>/HashTable/Dictionary)
- 一起谈.NET技术,数组排序方法的性能比较(中):Array.Sort<T> 实现分析
- [C#]Array.Sort和List<T>.Sort的委托版本比IComparer版本效率低
- C#中的[],Array,List<>,ArrayList的区别
- C# 中的集合(Array/ArrayList/List<T>/HashTable/Dictionary)
- 数组排序方法的性能比较(2):Array.Sort<T>实现分析
- C# 中的集合(Array/ArrayList/List<T>/HashTable/Dictionary)
- List<T>在搜索和排序时采用不同方法的性能比较