public static class Program {
public static void Main() {
ValueTypeTest();
ReferenceTypeTest();
Console.ReadLine();
}
private static void ValueTypeTest() {
const Int32 count = 10000000;
using (new OperatorTimer("泛型值类型:List<Int32>")) {
List<Int32> l = new List<int>();
for (int i = 0; i < count; i++) {
l.Add(i);
Int32 x = l[i];
}
l = null;
}
using (new OperatorTimer("非泛型值类型:ArrayList of Int32")) {
ArrayList a = new ArrayList();
for (int i = 0; i < count; i++) {
a.Add(i);
Int32 x = (Int32)a[i];
}
a = null;
}
}
private static void ReferenceTypeTest() {
const Int32 count = 10000000;
using (new OperatorTimer("泛型类型引用类型:List<String>")) {
List<String> l = new List<string>();
for (int i = 0; i < count; i++) {
l.Add("X");
String x = l[i];
}
l = null;
}
using (new OperatorTimer("非泛型类型引用类型:ArrayList of String")) {
ArrayList a = new ArrayList();
for (int i = 0; i < count; i++) {
a.Add("X");
String x = (String)a[i];
}
a = null;
}
}
}
internal sealed class OperatorTimer : IDisposable {
private Int64 m_starttime;
private String m_text;
private Int32 m_collectionCount;
public OperatorTimer(String t) {
PrepareForOperation();
m_text = t;
m_collectionCount = GC.CollectionCount(0);
m_starttime = Stopwatch.GetTimestamp();
}
public void Dispose() {
Console.WriteLine("{0,6:###.00} seconds (GCs={1,3}){2}",
(Stopwatch.GetTimestamp() - m_starttime) / (Double)Stopwatch.Frequency,
GC.CollectionCount(0) - m_collectionCount,
m_text);
}
public static void PrepareForOperation() {
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
}
}
16.1FCL中的泛型public static void Main() {
ValueTypeTest();
ReferenceTypeTest();
Console.ReadLine();
}
private static void ValueTypeTest() {
const Int32 count = 10000000;
using (new OperatorTimer("泛型值类型:List<Int32>")) {
List<Int32> l = new List<int>();
for (int i = 0; i < count; i++) {
l.Add(i);
Int32 x = l[i];
}
l = null;
}
using (new OperatorTimer("非泛型值类型:ArrayList of Int32")) {
ArrayList a = new ArrayList();
for (int i = 0; i < count; i++) {
a.Add(i);
Int32 x = (Int32)a[i];
}
a = null;
}
}
private static void ReferenceTypeTest() {
const Int32 count = 10000000;
using (new OperatorTimer("泛型类型引用类型:List<String>")) {
List<String> l = new List<string>();
for (int i = 0; i < count; i++) {
l.Add("X");
String x = l[i];
}
l = null;
}
using (new OperatorTimer("非泛型类型引用类型:ArrayList of String")) {
ArrayList a = new ArrayList();
for (int i = 0; i < count; i++) {
a.Add("X");
String x = (String)a[i];
}
a = null;
}
}
}
internal sealed class OperatorTimer : IDisposable {
private Int64 m_starttime;
private String m_text;
private Int32 m_collectionCount;
public OperatorTimer(String t) {
PrepareForOperation();
m_text = t;
m_collectionCount = GC.CollectionCount(0);
m_starttime = Stopwatch.GetTimestamp();
}
public void Dispose() {
Console.WriteLine("{0,6:###.00} seconds (GCs={1,3}){2}",
(Stopwatch.GetTimestamp() - m_starttime) / (Double)Stopwatch.Frequency,
GC.CollectionCount(0) - m_collectionCount,
m_text);
}
public static void PrepareForOperation() {
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
}
}
16.2Wintellect的Power Collections库
为了使CLR编程人员也能使用C++标准模板库的部分集合类,Wintellect制作了Power Collections库。可从Wintellect.com下载。
16.3泛型基础结构