C#高性能動態獲取對象屬性值
C#高性能動態獲取對象屬性值
ZKEASOFT December 04, 2020
動態獲取對象的性能值,這個在開發過程中經常會遇到,這里我們探討一下何如高性能的獲取屬性值。為了對比測試,我們定義一個類People
public class People
{
public string Name { get; set; }
}
然后通過直接代碼調用方式來取1千萬次看要花多少時間:
private static void Directly()
{
People people = new People { Name = "Wayne" };
Stopwatch stopwatch = Stopwatch.StartNew();
for (int i = 0; i < 10000000; i++)
{
object value = people.Name;
}
stopwatch.Stop();
Console.WriteLine("Directly: {0}ms", stopwatch.ElapsedMilliseconds);
}
大概花了37ms:

反射
通過反射來獲取對象的屬性值,這應該是大家常用的方式,但這種方式的性能比較差。接下來我們來看看同樣取1千萬次需要多少時間:
private static void Reflection()
{
People people = new People { Name = "Wayne" };
Type type = typeof(People);
PropertyInfo property = type.GetProperty("Name");
Stopwatch stopwatch = Stopwatch.StartNew();
for (int i = 0; i < 10000000; i++)
{
object value = property.GetValue(people);
}
stopwatch.Stop();
Console.WriteLine("Reflection: {0}ms", stopwatch.ElapsedMilliseconds);
}
大概花了1533ms,果然要慢很多:

那既然反射慢,那還有沒有其它方式呢?
動態構建Lambda
我們知道可以動態構建Linq的Lambda表達式,然后通過編譯后得到一個委托,如果能動態構建返回屬性值的委托,就可以取到值了。所以我們想辦法構建一個像這樣的委托:
Func<People, object> getName = m => m.Name;
接下來我們就通過Expression來構建:
private static void Lambda()
{
People people = new People { Name = "Wayne" };
Type type = typeof(People);
var parameter = Expression.Parameter(type, "m");//參數m
PropertyInfo property = type.GetProperty("Name");
Expression expProperty = Expression.Property(parameter, property.Name);//取參數的屬性m.Name
var propertyDelegateExpression = Expression.Lambda(expProperty, parameter);//變成表達式 m => m.Name
var propertyDelegate = (Func<People, object>)propertyDelegateExpression.Compile();//編譯成委托
Stopwatch stopwatch = Stopwatch.StartNew();
for (int i = 0; i < 10000000; i++)
{
object value = propertyDelegate.Invoke(people);
}
stopwatch.Stop();
Console.WriteLine("Lambda:{0}ms", stopwatch.ElapsedMilliseconds);
}
然后我們測試一下,大概花了138ms,性能要比反射好非常多:

委托調用
雖然動態構建Lambda的性能已經很好了,但還是更好嗎?畢竟比直接調用還是差了一些,要是能直接調用屬性的取值方法就好了。
在C#中,可讀屬性都有一個對應的get_XXX()的方法,可以通過調用這個方法來取得對應屬性的值。可以使用System.Delegate.CreateDelegate創建一個委托來調用這個方法。
- 通過委托調用方法來取得屬性值
我們定義一個MemberGetDelegate的委托,然后通過它來調用取值方法:
delegate string MemberGetDelegate(People p);
private static void Delegate()
{
People people = new People { Name = "Wayne" };
Type type = typeof(People);
PropertyInfo property = type.GetProperty("Name");
MemberGetDelegate memberGet = (MemberGetDelegate)System.Delegate.CreateDelegate(typeof(MemberGetDelegate), property.GetGetMethod());
Stopwatch stopwatch = Stopwatch.StartNew();
for (int i = 0; i < 10000000; i++)
{
object value = memberGet(people);
}
stopwatch.Stop();
Console.WriteLine("Delegate: {0}ms", stopwatch.ElapsedMilliseconds);
}
然后我們測試一下,大概花了38ms,性能幾乎與直接調用一致:

但這個方法的限制在于需要知道返回類型,在返回類型未知的時候就顯得不那么方便了。
Emit
Emit可以在運行時動態生成代碼,我們可以動態構建一個方法,在這個方法里面調用取屬性值的方法:
private static void Emit()
{
People people = new People { Name = "Wayne" };
Type type = typeof(People);
var property = type.GetProperty("Name");
DynamicMethod method = new DynamicMethod("GetPropertyValue", typeof(object), new Type[] { type }, true);
ILGenerator il = method.GetILGenerator();
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Callvirt, property.GetGetMethod());
if (property.PropertyType.IsValueType)
{
il.Emit(OpCodes.Box, property.PropertyType);//值類型需要裝箱,因為返回類型是object
}
il.Emit(OpCodes.Ret);
Func<People, object> fun = method.CreateDelegate(typeof(Func<People, object>)) as Func<People, object>;
Stopwatch stopwatch = Stopwatch.StartNew();
for (int i = 0; i < 10000000; i++)
{
object value = fun.Invoke(people);
}
stopwatch.Stop();
Console.WriteLine("Emit:{0}ms", stopwatch.ElapsedMilliseconds);
}
測試一下,大概119ms,性能與動態構建Lambda編譯的委托接近


浙公網安備 33010602011771號