在.net framework 2.0之前,数组表示可以用Array 和集合类 ArrayList 表示,2.0后,.net 引进了泛型的概念List<>,那么我们的选择就多了一种。
2.0引进泛型其本意是减少类型的装箱拆箱所带来的性能消耗。
比如;
private void CompareTo
{
....
}
这里的List
今天主要讨论的是在类型确定的情况下,哪种性能高一些,ArrayList就不用考虑了,它肯定是最慢的,它只能add object type的.这里主要讨论List<>和Array的性能:
先看string的情况:
static void Main(string[] args)
{
const int COUNT=1000;
string[] array = new string[COUNT];
List
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
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.