数组、LIst<> 、 ArrayList的性能对比
staticvoidMain(string[]args){Stopwatchsw=newStopwatch();int[]intArray=newint[100];sw.Start();for(inti=0;i<100;i++){intArray[i]=i;}sw.Stop();Console.WriteLine("Add0~100toint[100]:"+sw.Elapsed);ArrayListlist=newArrayList();sw=newStopwatch();sw.Start();for(inti=0;i<100;i++){list.Add(i);}sw.Stop();Console.WriteLine("Add0~100toArrayList:"+sw.Elapsed);List<int>intList=newList<int>();sw=newStopwatch();sw.Start();for(inti=0;i<100;i++){intList.Add(i);}sw.Stop();Console.WriteLine("Add0~100toList<int>:"+sw.Elapsed);Console.ReadLine();}
效果如图:
可以看到数组明显比较快,但是必需初始化长度
目测原因是往ArrayList中添加元素时发生了装箱操作
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。