.NET 2.0下反射和CodeDom访问对象属性性能测试比较
这段时间在编写基于CodeDom动太操作对象属性的组件,为了证明一下效率于是和反射操作进行简单的测试比较。测试主要分为两部分:属性设置和属性获取。分别测试了硬编码、CodeDom动态调和反射三种情况比较。
属性设置测试:
测试代码:
Console.SetOut(new System.IO.StreamWriter("c:\\Test.log"));
NorthWind.Entities.Employees emp = new NorthWind.Entities.Employees();
PropertyInfo id = typeof(NorthWind.Entities.Employees).GetProperty("EmployeeID");
PropertyInfo fn = typeof(NorthWind.Entities.Employees).GetProperty("FirstName");
DynamicInvokes.PropertyCollection pas = DynamicInvokes.Invoker.GetProperties(typeof(NorthWind.Entities.Employees));
System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
emp.FirstName = "FAN";
for (int i = 0; i < 10; i++)
{
Console.WriteLine("-------------------------属性设置("+i+")---------------------------");
watch.Reset();
watch.Start();
emp.EmployeeID = 2;
watch.Stop();
Console.WriteLine("更编码:\t\t" + watch.ElapsedTicks);
watch.Reset();
watch.Start();
pas[id][emp] = 2;
watch.Stop();
Console.WriteLine( "CodeDom:\t" + watch.ElapsedTicks);
watch.Reset();
watch.Start();
id.SetValue(emp, 2, null);
watch.Stop();
Console.WriteLine( "反射:\t\t" + watch.ElapsedTicks);
}
测试结果(1:100毫微秒,不要头尾结果)
-------------------------属性设置(1)---------------------------
更编码: 69
CodeDom: 117
反射: 100
-------------------------属性设置(2)---------------------------
更编码: 70
CodeDom: 71
反射: 94
-------------------------属性设置(3)---------------------------
更编码: 73
CodeDom: 70
反射: 96
-------------------------属性设置(4)---------------------------
更编码: 65
CodeDom: 70
反射: 92
-------------------------属性设置(5)---------------------------
更编码: 66
CodeDom: 70
反射: 96
-------------------------属性设置(6)---------------------------
更编码: 67
CodeDom: 70
反射: 95
-------------------------属性设置(7)---------------------------
更编码: 68
CodeDom: 70
反射: 93
-------------------------属性设置(8)---------------------------
更编码: 68
CodeDom: 71
反射: 94
属性获取测试:
测试代码:
object getvalue;
for (int i = 0; i < 10; i++)
{
Console.WriteLine("-------------------------获取设置(" + i + ")---------------------------");
watch.Reset();
watch.Start();
getvalue = emp.FirstName;
watch.Stop();
Console.WriteLine("更编码:\t\t" + watch.ElapsedTicks);
watch.Reset();
watch.Start();
getvalue = pas[fn][emp];
watch.Stop();
Console.WriteLine("CodeDom:\t" + watch.ElapsedTicks);
watch.Reset();
watch.Start();
getvalue= fn.GetValue(emp, null);
watch.Stop();
Console.WriteLine("反射:\t\t" + watch.ElapsedTicks);
}
测试结果(1:100毫微秒,不要头尾结果)
-------------------------获取设置(1)---------------------------
更编码: 69
CodeDom: 113
反射: 90
-------------------------获取设置(2)---------------------------
更编码: 68
CodeDom: 75
反射: 88
-------------------------获取设置(3)---------------------------
更编码: 68
CodeDom: 69
反射: 88
-------------------------获取设置(4)---------------------------
更编码: 69
CodeDom: 72
反射: 88
-------------------------获取设置(5)---------------------------
更编码: 68
CodeDom: 72
反射: 90
-------------------------获取设置(6)---------------------------
更编码: 69
CodeDom: 73
反射: 90
-------------------------获取设置(7)---------------------------
更编码: 67
CodeDom: 72
反射: 88
-------------------------获取设置(8)---------------------------
更编码: 66
CodeDom: 73
反射: 87
根据lone描述场景测试结果:
测试代码
for (int i = 0; i < 10; i++)
{
Console.WriteLine("-------------------------硬编码属性设置(" + i + ")---------------------------");
watch.Reset();
watch.Start();
for (int k = 0; k < 1000; k++)
{
emp = new NorthWind.Entities.Employees();
emp.EmployeeID = k;
emp.FirstName ="henry" + k;
}
watch.Stop();
Console.WriteLine(watch.ElapsedTicks);
}
for (int i = 0; i < 10; i++)
{
Console.WriteLine("-------------------------CodeDom属性设置(" + i + ")---------------------------");
watch.Reset();
watch.Start();
for (int k = 0; k < 1000; k++)
{
emp = new NorthWind.Entities.Employees();
pas[id][emp] = k;
pas[fn][emp] = "henry" + k;
}
watch.Stop();
Console.WriteLine(watch.ElapsedTicks);
}
for (int i = 0; i < 10; i++)
{
Console.WriteLine("-------------------------反射属性设置(" + i + ")---------------------------");
watch.Reset();
watch.Start();
for (int k = 0; k < 1000; k++)
{
emp = new NorthWind.Entities.Employees();
id.SetValue(emp, k, null);
fn.SetValue(emp, "henry" + k, null);
}
watch.Stop();
Console.WriteLine(watch.ElapsedTicks);
}
测试结果(1:100毫微秒,不要头尾结果)
------------------------属性设置(1)---------------------------
3864硬编码
8249 CodeDom
36580反射
-------------------------属性设置(2)---------------------------
3770 硬编码
8259 CodeDom
38693 反射
-------------------------属性设置(3)---------------------------
3803 硬编码
8569 CodeDom
36047 反射
-------------------------属性设置(4)---------------------------
3780 硬编码
8404 CodeDom
36452 反射
-------------------------属性设置(5)---------------------------
3755 硬编码
8253 CodeDom
44152 反射
-------------------------属性设置(6)---------------------------
3896 硬编码
13130 CodeDom
34827 反射
-------------------------属性设置(7)---------------------------
3782 硬编码
8321 CodeDom
34637 反射
-------------------------属性设置(8)---------------------------
3864 硬编码
8360 CodeDom
40850 反射