泛型集合类的优势好象还是类型安全,性能方面好象没什么优势
下面那个测试写得有点急,写错了。
但不可理解的是明明ArrayList比List<T>多一步装箱操作,但测试结果确不能让人满意。
class Program

...{
static void Main(string[] args)

...{
Console.WriteLine("Constructor Time:");
Program.TestConstruct();
Console.WriteLine();
Console.WriteLine("AddMechod Time:");
Program.TestAddMethod();
Console.WriteLine();
Console.WriteLine("Loop Add Time:");
Program.Test(10);
Program.Test(100);
Program.Test(1000);
Program.Test(1000 * 1000);
Program.Test(Int32.MaxValue);
Console.ReadLine();
}

private static void Test(int times)

...{
SomeValueTye temp;
IList<SomeValueTye> list = new List<SomeValueTye>();
ArrayList al = new ArrayList();
Stopwatch sw = new Stopwatch();
//List<T>,add操作
sw.Start();
for (int i = 0; i < times; i++)

...{
list.Add(new SomeValueTye(i, "test"));
}
sw.Stop();
Console.WriteLine("IL In" + " " + sw.Elapsed + " " + times.ToString());
sw.Reset();
//ArrayList, add操作
sw.Start();
for (int i = 0; i < times; i++)

...{
al.Add(new SomeValueTye(i, "test"));
}
sw.Stop();
Console.WriteLine("AL In" + " " + sw.Elapsed + " " + times.ToString());

sw.Reset();

//List<T>,取值
sw.Start();
for (int i = 0; i < times; i++)

...{
temp = list[i];
}
sw.Stop();
Console.WriteLine("IL Out" + " " + sw.Elapsed + " " + times.ToString());

sw.Reset();

//ArrayList,取值
sw.Start();
for (int i = 0; i < times; i++)

...{
temp = (SomeValueTye)al[i];
}
sw.Stop();
Console.WriteLine("AL Out" + " " + sw.Elapsed + " " + times.ToString());
Console.WriteLine();
}

private static void TestConstruct()

...{
Stopwatch sw = new Stopwatch();
sw.Start();
for (int i = 0; i < 1000 * 1000; i++)

...{
IList<SomeValueTye> list = new List<SomeValueTye>(10);
}
sw.Stop();
Console.WriteLine("IL: " + sw.Elapsed);

sw.Reset();

sw.Start();
for (int i = 0; i < 1000 * 1000; i++)

...{
ArrayList al = new ArrayList(10);
}
sw.Stop();
Console.WriteLine("AL: " + sw.Elapsed);
}

private static void TestAddMethod()

...{
Stopwatch sw = new Stopwatch();
IList<SomeValueTye> list = new List<SomeValueTye>();
sw.Start();
for(int i = 0; i < 1000*1000; i++)
list.Add(new SomeValueTye(1,"test"));
sw.Stop();
Console.WriteLine("IL: " + sw.Elapsed);

sw.Reset();
ArrayList al = new ArrayList();
sw.Start();
for (int i = 0; i < 1000 * 1000; i++)
al.Add(new SomeValueTye(1, "test"));
sw.Stop();
Console.WriteLine("AL: " + sw.Elapsed);
}
}

struct SomeValueTye

...{
public int m_TestInt;
public string m_TestStr;
public SomeValueTye(int intValue, string strValue)

...{
m_TestInt = intValue;
m_TestStr = strValue;
}
}
运行结果:
Constructor Time:
IL: 00:00:00.1989778
AL: 00:00:00.1317960
AddMechod Time:
IL: 00:00:00.0932551
AL: 00:00:00.2032006
|
1
|
10
|
100
|
1000
|
1000000
|
List<T>
|
572
|
212
|
430
|
782
|
1142039
|
ArrayList
|
164
|
162
|
215
|
1206
|
2112237
|
IL_0020: newobj instance void IList.SomeValueTye::.ctor(int32,
string)

IL_0025: callvirt instance void /**//* UNKNOWN TYPE (0x15)*/::Add(!0)
IL_0083: newobj instance void IList.SomeValueTye::.ctor(int32,
string)
IL_0088: box IList.SomeValueTye
IL_008d: callvirt instance int32 [mscorlib]System.Collections.ArrayList::Add(object)
Why?