一直就听说装箱很影响性能,今天忍不住试试,见如下代码:
Code
ArrayList ba = new ArrayList();
List<int> bs = new List<int>();
DateTime t = DateTime.Now;
for (int i=0 ;i<1000000;i++)
{
ba.Add(i);
}
Console.WriteLine("time:{0}", DateTime.Now.Millisecond - t.Millisecond );
t = DateTime.Now;
for (int i = 0; i < 1000000; i++)
{
bs.Add(i);
}
Console.WriteLine("time:{0}", DateTime.Now.Millisecond - t.Millisecond);
看见结果是125 VS 16左右,差了尽10倍,真吓一条,怪不得,看来以前的系统中hashtable和arraylist都要改用Dictionary<T,K> HashSet<T,K>
List<T>了。