C# List<>与Array性能比较
2011-07-19 09:51
513 查看
在.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:77
the value of Total for list:0
list init time:218(不同的硬件配置会有不同的结果)从上面的结果来看array的效率要比List的要高一些,当数组长度不是很大时,两者没什么区别,建议用List<>,毕竟是可变长度,可以Add;特殊应用还是建议用array,不提倡用ArrayList.
比如;
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:77
the value of Total for list:0
list init time:218(不同的硬件配置会有不同的结果)从上面的结果来看array的效率要比List的要高一些,当数组长度不是很大时,两者没什么区别,建议用List<>,毕竟是可变长度,可以Add;特殊应用还是建议用array,不提倡用ArrayList.
相关文章推荐
- C# List<>与Array性能比较
- C# List<>与Array性能比较
- C# List<>与Array性能比较
- C#中list<T>性能内存优化分配,Capacity设置
- 粗谈C#里 dictionary,HashTable,List<T>,Array 的性能优缺和实现原理。
- C# 数组集合 <一>基本概念(Array&ArrayList&List<T>&SortList&LinkList的实现Code)
- ArrayList Array List<T>性能比较
- Array,ArrayList 和 List<T>的选择和性能比较.
- List<T>在搜索和排序时采用不同方法的性能比较
- C#.Net:List<T>集合列表的Linq语句查询示例
- C# List<T>
- C# List<T>用法
- 数组排序方法的性能比较(2):Array.Sort<T>实现分析
- C# List<T>用法详解
- C# List<T> 泛型的反射, 泛型 函数的反射
- [c#基础教程]C#泛型列表List<T>基本用法总结
- C# IEnumerable<T>、IEnumerator<T>、List<T>、ArrayList、[]数组各各的区别
- C# 哈希表Hashtable与字典表Dictionary<K,V>的比较。
- 【C#】List<T>类型强制转换
- C# List<T>基本用法