使用C#时,我注意到使用动态生成类型的实例与简单结构填充列表时的性能差异。下面的代码包含4种用于使用100,000个对象填充列表的不同方法。动态创建类型的性能
每种方法进行不同的:
Button1的:15毫秒
将Button2:31毫秒
将Button3 & 4:300毫秒
注意,对于按钮3的代码& 4从来到this topic
任何人都可以解释为什么动态创建的对象更慢?
public struct DataRow
{
public double t;
public double vf;
public double im;
public double T { get { return t; } set { t = value; } }
public double Vf { get { return vf; } set { vf = value; } }
public double Im { get { return im; } set { im = value; } }
}
//Use struct defined above
private void button1_Click(object sender, EventArgs e)
{
int n = 0;
//adding rows
List<DataRow> myTable = new List<DataRow>();
DataRow myRow = new DataRow();
start = DateTime.Now;
while (n < 100000)
{
myRow.T = n * 1.0;
myRow.Vf = 2.0;
myRow.Im = 4.0;
myTable.Add(myRow);
n++;
}
end = DateTime.Now;
System.TimeSpan diff = end.Subtract(start);
label2.Text = diff.Seconds.ToString();
label4.Text = diff.Milliseconds.ToString();
dataGridView1.DataSource = myTable;
}
//define the list as it is done on buttons 3 & 4 but use the static struct
private void button2_Click(object sender, EventArgs e)
{
Type myType = typeof(DataRow);
Type listType = typeof(List<>);
Type myListType = listType.MakeGenericType(myType);
IList myTable = (IList)Activator.CreateInstance(myListType);
DataRow bRow = new DataRow();
int n = 0;
start = DateTime.Now;
while (n < 100000)
{
bRow.t = n * 1.0;
bRow.vf = 2.0;
bRow.im = 4.0;
myTable.Add(bRow);
n++;
}
end = DateTime.Now;
System.TimeSpan diff = end.Subtract(start);
label2.Text = diff.Seconds.ToString();
label4.Text = diff.Milliseconds.ToString();
dataGridView1.DataSource = myTable;
}
//Create assy at runtime and load dll
private void button3_Click(object sender, EventArgs e)
{
Type myType = CreateDynRow();
Assembly myAssy = Assembly.LoadFrom("DynaRowAssy.dll");
Type myRow = myAssy.GetType("DynaRow");
Type listType = typeof(List<>);
Type myListType = listType.MakeGenericType(myRow);
IList myTable = (IList)Activator.CreateInstance(myListType);
FieldInfo piT = myRow.GetField("t");
FieldInfo piVf = myRow.GetField("vf");
FieldInfo piIm = myRow.GetField("im");
ValueType aRow = (ValueType)Activator.CreateInstance(myRow);
int n = 0;
start = DateTime.Now;
while (n < 100000)
{
piT.SetValue(aRow, 1 * n);
piVf.SetValue(aRow, 2.0);
piIm.SetValue(aRow, 4.0);
myTable.Add(aRow);
n++;
}
end = DateTime.Now;
System.TimeSpan diff = end.Subtract(start);
label2.Text = diff.Seconds.ToString();
label4.Text = diff.Milliseconds.ToString();
dataGridView1.DataSource = myTable;
}
//create assy at runtime in memory
private void button4_Click(object sender, EventArgs e)
{
//build the assembly
Type myType = CreateDynRow();
Type listType = typeof(List<>);
Type myListType = listType.MakeGenericType(myType);
IList myTable = (IList)Activator.CreateInstance(myListType);
FieldInfo piT = myType.GetField("t");
FieldInfo piVf = myType.GetField("vf");
FieldInfo piIm = myType.GetField("im");
ValueType aRow = (ValueType)Activator.CreateInstance(myType);
int n = 0;
start = DateTime.Now;
while (n < 100000)
{
piT.SetValue(aRow, 1 * n);
piVf.SetValue(aRow, 2.0);
piIm.SetValue(aRow, 4.0);
myTable.Add(aRow);
n++;
}
end = DateTime.Now;
System.TimeSpan diff = end.Subtract(start);
label2.Text = diff.Seconds.ToString();
label4.Text = diff.Milliseconds.ToString();
dataGridView1.DataSource = myTable;
}
只是一个提示:DateTime的分辨率大约是15毫秒,所以当你使用DateTime.Now来计时的时候,两个实际上执行得非常相似的事情可能会出现不同的长达15ms,只是由于舍入的略有不同边界。因此,如果您的基准测试在15-30ms左右运行,则要么运行更多迭代(以便15ms舍入误差变得微不足道),要么使用System.Diagnostics.Stopwatch等更高分辨率的计时器(后者稍微更方便一些API也是!)。 – itowlson 2009-12-21 19:43:10