C#List与Array性能⽐较
在 framework 2.0之前,数组表⽰可以⽤Array 和集合类 表⽰,2.0后, 引进了泛型的概念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
writeline方法属于类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.
引⽤⾃cnblogs的⽂章。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论