C#高效能動態獲取物件屬性值
阿新 • • 發佈:2020-12-10
動態獲取物件的效能值,這個在開發過程中經常會遇到,這裡我們探討一下何如高效能的獲取屬性值。為了對比測試,我們定義一個類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 (inti = 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 = newPeople { 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編譯的委託接近
文章轉載自http://www.zkea.net/codesnippet/detail/csharp-fast-get-property-value.html