C#을 사용하여 동적으로 생성 된 유형의 인스턴스와 간단한 구조체의 인스턴스로 목록을 채울 때 성능에 큰 차이가 있음을 눈치 챘을 것입니다. 아래 코드에는 100,000 개 개체로 목록을 채우는 4 가지 방법이 포함되어 있습니다.동적으로 생성 된 유형의 성능
각있어서 다르게 수행
Button1에 300 밀리
참고 버튼 3 코드 & 4에서 온 15 밀리
Button2를 : 4 & 31 밀리
Button3을을 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