DefconDotNet

Reflection Delegate

Mar 28th, 2013
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.38 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.Reflection;
  6. using System.Reflection.Emit;
  7. using System.Text;
  8. using Fasterflect;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Reflection;
  14. using System.Xml;
  15.  
  16.  
  17. namespace Optimizer
  18. {
  19.     class Program
  20.     {
  21.         static void Main(string[] args)
  22.         {
  23.             FasterflectTest();
  24.         }
  25.  
  26.         #region Fasterflect
  27.  
  28.         static void FasterflectTest()
  29.         {
  30.             var iterations = 100000;
  31.             var person = new Person { Id = 1, Name = "Filip Cornelissen" };
  32.  
  33.             var timer1 = Fasterflect_Reflection(iterations, person);
  34.             var timer2 = Fasterflect_Faster(iterations, person);
  35.             var timer3 = Fasterflect_Delegate(iterations, person);
  36.  
  37.  
  38.             Console.WriteLine("---------Fasterflect---------");
  39.             Console.WriteLine("timer2: " + timer2 + " (FasterFlect)");
  40.             Console.WriteLine("timer1: " + timer1 + " (Reflection)");
  41.             Console.WriteLine("timer1: " + timer3 + " (Delegate)");
  42.             Console.WriteLine("-Total: " + (timer1 + timer2 + timer3));
  43.             Console.WriteLine("-based on " + iterations + " iterations");
  44.             Console.WriteLine("-----------------------------");
  45.         }
  46.  
  47.         static TimeSpan Fasterflect_Faster(int iterations, Person person)
  48.         {
  49.             var timer = new Stopwatch();
  50.             timer.Start();
  51.  
  52.  
  53.             //var invoker = CreateDelegate(typeof(Person), "Name");
  54.             //var name = (string)invoker.Invoke(person);
  55.             //var x = new Fasterflect.MemberGetter(o => o.GetPropertyValue("Name"));
  56.             //Console.WriteLine((string)invoker.Invoke(person));
  57.  
  58.             for (var i = 0;i < iterations;i++)
  59.             {
  60.                 var pval = person.GetPropertyValue("Name") as string;
  61.                 if (pval != "Filip Cornelissen")
  62.                     throw new Exception("Wrong Value");
  63.             }
  64.  
  65.             return timer.Elapsed;
  66.         }
  67.  
  68.         static TimeSpan Fasterflect_Reflection(int iterations, Person person)
  69.         {
  70.             var timer = new Stopwatch();
  71.             timer.Start();
  72.  
  73.             var modelType = typeof(Person);
  74.             var columns = modelType.GetProperties();
  75.             var prop = columns.SingleOrDefault(info => info.Name == "Name");
  76.  
  77.             if (prop != null)
  78.                 for (var i = 0;i < iterations;i++) {
  79.                     var pval = prop.GetValue(person, null) as string;
  80.                     if (pval != "Filip Cornelissen")
  81.                         throw new Exception("Wrong Value");
  82.                 }
  83.  
  84.             return timer.Elapsed;
  85.         }
  86.  
  87.         static TimeSpan Fasterflect_Delegate(int iterations, Person person)
  88.         {
  89.             var timer = new Stopwatch();
  90.             timer.Start();
  91.  
  92.             var modelType = typeof(Person);
  93.             var columns = modelType.GetProperties();
  94.             var prop = columns.SingleOrDefault(info => info.Name == "Name");
  95.            
  96.             if (prop != null)
  97.             {
  98.                 var f = Func(person, prop);
  99.  
  100.                 for (var i = 0;i < iterations;i++) {
  101.                     var pval = Func(person, prop)();
  102.                     if (pval != "Filip Cornelissen")
  103.                         throw new Exception("Wrong Value");
  104.                 }
  105.             }
  106.  
  107.             return timer.Elapsed;
  108.         }
  109.  
  110.         private static Func<string> Func(Person person, PropertyInfo prop)
  111.         {
  112.             return (Func<string>)Delegate.CreateDelegate(typeof(Func<string>), person, prop.GetGetMethod());
  113.         }
  114.  
  115.         //public class PropertyReference<T, TM> //: IPropertyReference
  116.         //{
  117.         //    public string ComponentName { get; set; }
  118.         //    public string PropertyName { get; set; }
  119.         //    public bool IsInitialized
  120.         //    {
  121.         //        get
  122.         //        {
  123.         //            return (_action != null && _func != null);
  124.         //        }
  125.         //    }
  126.  
  127.         //    Action<T> _action;
  128.         //    Func<TM, T> _func;
  129.  
  130.         //    public PropertyReference() { }
  131.  
  132.         //    public PropertyReference(string componentName, string propertyName)
  133.         //    {
  134.         //        ComponentName = componentName;
  135.         //        PropertyName = propertyName;
  136.         //    }
  137.  
  138.         //    public void Initialize()
  139.         //    {
  140.         //        //Object component = e.GetByName(ComponentName);
  141.         //        //if (component == null) return;
  142.  
  143.         //        //Type t = e.GetByName(ComponentName).GetType();
  144.  
  145.  
  146.         //        var modelType = typeof(TM);
  147.         //        var columns = modelType.GetProperties();
  148.         //        //var prop = columns.SingleOrDefault(info => info.Name == "Name");
  149.  
  150.         //        var prop = modelType.GetProperty(PropertyName);
  151.  
  152.         //        _func = (Func<TM, T>)Delegate.CreateDelegate(typeof(Func<TM, T>), Func<tm>, prop.GetGetMethod());
  153.         //    }
  154.  
  155.         //    public void Reset()
  156.         //    {
  157.         //        _action = null;
  158.         //        _func = null;
  159.  
  160.         //    }
  161.  
  162.         //    public void Set(T value)
  163.         //    {
  164.         //        _action.Invoke(value);
  165.         //    }
  166.  
  167.         //    public T Get(TM tm)
  168.         //    {
  169.         //        return _func(tm);
  170.         //    }
  171.         //}
  172.  
  173.  
  174.         //public static MethodInvoker CreateDelegate(Type targetType, string methodName)
  175.         //{
  176.         //    var method = new DynamicMethod("invoke",
  177.         //                                   MethodAttributes.Static | MethodAttributes.Public,
  178.         //                                   CallingConventions.Standard,
  179.         //                                   typeof (object), new Type[0], targetType, true);
  180.            
  181.         //    var generator = method.GetILGenerator();
  182.         //    var methodInfo = targetType.GetMethod(methodName, BindingFlags.Instance);
  183.  
  184.         //    generator.Emit(OpCodes.Ldarg_0);
  185.         //    generator.Emit(OpCodes.Castclass, targetType);
  186.         //    generator.Emit(OpCodes.Callvirt, methodInfo);
  187.  
  188.         //    if (methodInfo.ReturnType == typeof(void)) {
  189.         //        generator.Emit(OpCodes.Ldnull);
  190.         //    }
  191.         //    else {
  192.         //        if (methodInfo.ReturnType.IsValueType) {
  193.         //            generator.Emit(OpCodes.Box, methodInfo.ReturnType);
  194.         //        }
  195.         //    }
  196.  
  197.         //    generator.Emit(OpCodes.Ret);
  198.         //    return method.DelegateForCallMethod();
  199.         //}
  200.  
  201.         #endregion
  202.  
  203.         #region Mod vs Not
  204.  
  205.         //-----------------------------
  206.         //timer1: 00:00:02.4982942 (not)
  207.         //timer2: 00:00:03.4878165 (mod)
  208.         //-Total: 00:00:05.9861107
  209.         //-based on 1.000.000.000 iterations
  210.         //-----------------------------
  211.        
  212.         static void ModOrNot()
  213.         {
  214.             var iterations = 1000000000;
  215.  
  216.             var timer1 = ModVsNot_Not(iterations);
  217.             var timer2 = ModVsNot_Mod(iterations);
  218.  
  219.  
  220.             Console.WriteLine("-----------------------------");
  221.             Console.WriteLine("timer1: " + timer1 + " (not)");
  222.             Console.WriteLine("timer2: " + timer2 + " (mod)");
  223.             Console.WriteLine("-Total: " + (timer1 + timer2));
  224.             Console.WriteLine("-based on " + iterations + " iterations");
  225.             Console.WriteLine("-----------------------------");
  226.         }
  227.  
  228.         private static TimeSpan ModVsNot_Not(int iterations)
  229.         {
  230.             var timer = new Stopwatch();
  231.             timer.Start();
  232.  
  233.             var oddRow = false;
  234.  
  235.             int x;
  236.             for (var i = 0;i <= iterations;i++) {
  237.                 x = (oddRow ? 1 : 0);
  238.                 oddRow = !oddRow;
  239.             }
  240.  
  241.             return timer.Elapsed;
  242.         }
  243.  
  244.         private static TimeSpan ModVsNot_Mod(int iterations)
  245.         {
  246.             var timer = new Stopwatch();
  247.             timer.Start();
  248.  
  249.             var sb = new StringBuilder();
  250.  
  251.             int x;
  252.             for (var i = 0;i <= iterations;i++) {
  253.                 x = ((i % 2 == 0) ? 1 : 0);
  254.             }
  255.  
  256.             return timer.Elapsed;
  257.         }
  258.  
  259.         #endregion
  260.     }
  261.  
  262.     public class Person
  263.     {
  264.         public int Id { get; set; }
  265.         public string Name { get; set; }
  266.     }
  267. }
Advertisement
Add Comment
Please, Sign In to add comment