接Killkill
http://blog.csdn.net/killlkilll/archive/2006/12/23/1457022.aspx
List<T>对值类型操作不需要进行装箱;ArrayList需要。
class
Program
...
{
static void Main(string[] args)
...{
Stopwatch sw = new Stopwatch();
sw.Start();
IList<SomeValueTye> list = new List<SomeValueTye>();
for (int i = 0; i < 1000; i++)
...{
list.Add(new SomeValueTye(i, "test"));
}
sw.Stop();
Console.WriteLine(sw.Elapsed);
sw.Reset();
ArrayList al = new ArrayList();
for (int i = 0; i < 1000; i++)
...{
al.Add(new SomeValueTye(i, "test"));
}
sw.Start();
sw.Stop();
Console.WriteLine(sw.Elapsed);
Console.ReadLine();
}
}

struct
SomeValueTye
...
{
public int m_TestInt;
public string m_TestStr;
public SomeValueTye(int intValue, string strValue)
...{
m_TestInt = intValue;
m_TestStr = strValue;
}
}
执行结果:
00:00:00.0022005
00:00:00.0000296
这就不是一个数量级的差别了。
本文通过实验对比了List<T>与ArrayList在处理值类型时的性能差异。实验结果显示,List<T>在操作值类型时由于不需要进行装箱与拆箱操作,其性能远高于ArrayList。
74

被折叠的 条评论
为什么被折叠?



