国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

關于反射優化的疑問,單次調用時直接反射要快于委托調用反射?

2019-11-17 03:14:41
字體:
來源:轉載
供稿:網友

關于反射優化的疑問,單次調用時直接反射要快于委托調用反射?

因為項目中需要用到反射,而單純反射據說性能很低,所以就想到了優化反射.而關于反射的優化方案園子里面有不少大牛都已經提出來詳細的解決方案,這個就不詳細說了,想了解的可以具體看這里http://m.survivalescaperooms.com/fish-li/category/453548.html.文章里面提到了通過創建委托來提高反射性能.

既然有了解決方案,那就來吧.

項目中主要用反射來操作對象屬性,所以就先不考慮方法的反射了

使用Emit構造委托

 1   public delegate object FastGetMethod(object arg);//定義委托類型 2   public static class FastReflection 3     { 4         /// <summary> 5         /// 緩存委托集合 6        /// </summary> 7        PRivate static ConcurrentDictionary<string, FastGetMethod> _getMethods = new ConcurrentDictionary<string, FastGetMethod>(); 8     public static FastGetMethod FastGetMethod(PropertyInfo property) 9        {10             FastGetMethod getMethod;11             if (!_getMethods.TryGetValue(string.Concat(property.DeclaringType.FullName, "_", property.Name), out getMethod))12             {13                 getMethod = FastReflection.GetGetMethod(property);14                 _getMethods.TryAdd(string.Concat(property.DeclaringType.FullName, "_", property.Name), getMethod);15             }16             return getMethod;17         }18     private static FastGetMethod GetGetMethod(PropertyInfo property)19         {20             if (property == null)21             {22                 throw new ArgumentNullException("property");23             }24             if (!property.CanRead)25             {26                 return null;27             }28             MethodInfo method = property.GetGetMethod(true);29             DynamicMethod dm = new DynamicMethod("GetValueMethod", typeof(object), new Type[] { typeof(object) }, property.DeclaringType, true);30             ILGenerator il = dm.GetILGenerator();31             if (!method.IsStatic)32             {33                 il.Emit(OpCodes.Ldarg_0);34                 il.EmitCall(OpCodes.Callvirt, method, null);35             }36             else37             {38                 il.EmitCall(OpCodes.Call, method, null);39             }40             if (property.PropertyType.IsValueType)41             {42                 il.Emit(OpCodes.Box, property.PropertyType);43             }44             il.Emit(OpCodes.Ret);45             return (FastGetMethod)dm.CreateDelegate(typeof(FastGetMethod));46         }47 }

如此,就來測試一下吧測試代碼如下

        class Person        {            public string Name { get; set; }            public int Age { get; set; }        }                    //調用代碼        Person p = new Person() { Age = 100, Name = "Hello Test" };            PropertyInfo info = (typeof(Person)).GetProperty("Age");            int times = 100 * 100 * 100;//調用1000000次            int temp1 = 0, temp2 = 0;            Console.WriteLine("調用{0}次比較", times);            Stopwatch watch1 = new Stopwatch();            watch1.Start();            FastGetMethod getMethod = FastReflection.FastGetMethod(info);            for (int i = 0; i < times; i++)            {                temp1 = (int)getMethod(p);            }            watch1.Stop();            Console.WriteLine("FastReflection耗時:{0}", watch1.Elapsed);            Stopwatch watch2 = new Stopwatch();            watch2.Start();            for (int i = 0; i < times; i++)            {                temp2 = (int)info.GetValue(p, null);            }            watch2.Stop();            Console.WriteLine("直接反射耗時:{0}",watch2.Elapsed);                        

在release模式下

可以看到性能上確實有了很大差別

到這里似乎就算是完了,可以突然想看看單次調用時性能上的差別有多少

結果卻有點出乎意料,單次調用時優化的反而比不上未優化的,讓人無法接受啊,是什么原因導致的呢?想到構建委托時要消耗性能,那就看下創建委托是需要的時間吧

可以看到構建委托時確實消耗不少性能,可是在接下來的調用還是有著巨大的差別,這是怎么一回事?單次調用直接反射有著巨大優勢,可是在多次調用是反而要比通過委托來調用,這究竟是怎么一回事呢?這個讓我很迷茫,這樣做到底算不算的上是優化?一個項目中一次請求中可能只掉用幾次反射,沒有像測試這樣一次性調用這么多次,那是否需要去反射呢?希望可以有高人幫忙解答下


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 定边县| 包头市| 鲜城| 厦门市| 华蓥市| 百色市| 密云县| 伽师县| 嘉鱼县| 肃宁县| 视频| 邯郸市| 涞水县| 永吉县| 台中县| 德安县| 金门县| 长春市| 洛南县| 平度市| 图们市| 莱阳市| 辉县市| 富蕴县| 灌南县| 成武县| 田东县| 黄梅县| 邵东县| 德阳市| 林周县| 海原县| 静乐县| 乐山市| 栾川县| 专栏| 孟村| 石柱| 德昌县| 和田县| 长沙县|