Performance of dynamically created type
Using C#, I am noticing a significant difference in perfomance when populating a list with instances of a dynamically generated type versus a simple struct. The code below includes 4 different methods for populating a list with 100,000 objects.
Each method performs differently:
Button1: 15 milliseconds
Button2: 31 milliseconds
Button3 & 4: 300 milliseconds
Note, the code for button 3 & 4 came from this topic
Could anyone explain why the dynamically created object is slower?
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; } }
}
//U开发者_开发问答se 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;
}
It's not (primarily) the dynamic creation: it's the use of reflection (FieldInfo.SetValue) that makes the button3 and button4 versions slower than when the calls can be compiled in.
A possible way around this is to declare an interface that your code can compile against, and have the dynamic type implement this interface. You'll still take a small hit instantiating and querying the dynamic type for that interface via reflection, but after that it should be as fast as the "static" references.
Simple Answer: More code is being executed to dynamicaly create the object.
Reflection is always going to be slower than defining the type up front and then working with that object directly. You're asking the runtime to do a lot more work for you rather than specifying everything up front. Depending on which Reflection features you're using...your code will be slower.
If you want the specifics, examine the IL that your code generates. That should give you the whole story.
Here's what we came up with. It is almost as fast as the statically defined case:
// Dynamically create DataRow derived from ValueType,
// List of DataRows,
// Delegates to properties
//
private void button4_Click(object sender, EventArgs e)
{
Type myType = CreateDynRow(); // dynamic version of DataRow, see above
Type myListType = typeof(List<>).MakeGenericType(myType);
IList myTable = (IList)Activator.CreateInstance(myListType);
ValueType myRowBuffer = (ValueType)Activator.CreateInstance(myType);
var mySet_TDelegate = myRowBuffer.GetInstanceInvoker<Action<Double>>("set_T");
var mySet_ImDelegate = myRowBuffer.GetInstanceInvoker<Action<Double>>("set_Im");
var mySet_VfDelegate = myRowBuffer.GetInstanceInvoker<Action<Double>>("set_Vf");
stopWatch.Reset();
stopWatch.Start();
for (int n = 0; n < rowCount; n++)
{
mySet_TDelegate(1.0 * n);
mySet_ImDelegate(4.0);
mySet_VfDelegate(2.0);
myTable.Add(myRowBuffer);
}
stopWatch.Stop();
label1.Text = String.Format("{0}", stopWatch.ElapsedMilliseconds);
dataGridView1.DataSource = myTable;
}
Thanks for your help. By the way, we got here using Justin's & Mark's answers. And GetInstanceInvoker is from Kenneth Xu's Common.Reflection class.
精彩评论