Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Reflection;
- using System.Reflection.Emit;
- using System.Text;
- using Fasterflect;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Reflection;
- using System.Xml;
- namespace Optimizer
- {
- class Program
- {
- static void Main(string[] args)
- {
- FasterflectTest();
- }
- #region Fasterflect
- static void FasterflectTest()
- {
- var iterations = 100000;
- var person = new Person { Id = 1, Name = "Filip Cornelissen" };
- var timer1 = Fasterflect_Reflection(iterations, person);
- var timer2 = Fasterflect_Faster(iterations, person);
- var timer3 = Fasterflect_Delegate(iterations, person);
- Console.WriteLine("---------Fasterflect---------");
- Console.WriteLine("timer2: " + timer2 + " (FasterFlect)");
- Console.WriteLine("timer1: " + timer1 + " (Reflection)");
- Console.WriteLine("timer1: " + timer3 + " (Delegate)");
- Console.WriteLine("-Total: " + (timer1 + timer2 + timer3));
- Console.WriteLine("-based on " + iterations + " iterations");
- Console.WriteLine("-----------------------------");
- }
- static TimeSpan Fasterflect_Faster(int iterations, Person person)
- {
- var timer = new Stopwatch();
- timer.Start();
- //var invoker = CreateDelegate(typeof(Person), "Name");
- //var name = (string)invoker.Invoke(person);
- //var x = new Fasterflect.MemberGetter(o => o.GetPropertyValue("Name"));
- //Console.WriteLine((string)invoker.Invoke(person));
- for (var i = 0;i < iterations;i++)
- {
- var pval = person.GetPropertyValue("Name") as string;
- if (pval != "Filip Cornelissen")
- throw new Exception("Wrong Value");
- }
- return timer.Elapsed;
- }
- static TimeSpan Fasterflect_Reflection(int iterations, Person person)
- {
- var timer = new Stopwatch();
- timer.Start();
- var modelType = typeof(Person);
- var columns = modelType.GetProperties();
- var prop = columns.SingleOrDefault(info => info.Name == "Name");
- if (prop != null)
- for (var i = 0;i < iterations;i++) {
- var pval = prop.GetValue(person, null) as string;
- if (pval != "Filip Cornelissen")
- throw new Exception("Wrong Value");
- }
- return timer.Elapsed;
- }
- static TimeSpan Fasterflect_Delegate(int iterations, Person person)
- {
- var timer = new Stopwatch();
- timer.Start();
- var modelType = typeof(Person);
- var columns = modelType.GetProperties();
- var prop = columns.SingleOrDefault(info => info.Name == "Name");
- if (prop != null)
- {
- var f = Func(person, prop);
- for (var i = 0;i < iterations;i++) {
- var pval = Func(person, prop)();
- if (pval != "Filip Cornelissen")
- throw new Exception("Wrong Value");
- }
- }
- return timer.Elapsed;
- }
- private static Func<string> Func(Person person, PropertyInfo prop)
- {
- return (Func<string>)Delegate.CreateDelegate(typeof(Func<string>), person, prop.GetGetMethod());
- }
- //public class PropertyReference<T, TM> //: IPropertyReference
- //{
- // public string ComponentName { get; set; }
- // public string PropertyName { get; set; }
- // public bool IsInitialized
- // {
- // get
- // {
- // return (_action != null && _func != null);
- // }
- // }
- // Action<T> _action;
- // Func<TM, T> _func;
- // public PropertyReference() { }
- // public PropertyReference(string componentName, string propertyName)
- // {
- // ComponentName = componentName;
- // PropertyName = propertyName;
- // }
- // public void Initialize()
- // {
- // //Object component = e.GetByName(ComponentName);
- // //if (component == null) return;
- // //Type t = e.GetByName(ComponentName).GetType();
- // var modelType = typeof(TM);
- // var columns = modelType.GetProperties();
- // //var prop = columns.SingleOrDefault(info => info.Name == "Name");
- // var prop = modelType.GetProperty(PropertyName);
- // _func = (Func<TM, T>)Delegate.CreateDelegate(typeof(Func<TM, T>), Func<tm>, prop.GetGetMethod());
- // }
- // public void Reset()
- // {
- // _action = null;
- // _func = null;
- // }
- // public void Set(T value)
- // {
- // _action.Invoke(value);
- // }
- // public T Get(TM tm)
- // {
- // return _func(tm);
- // }
- //}
- //public static MethodInvoker CreateDelegate(Type targetType, string methodName)
- //{
- // var method = new DynamicMethod("invoke",
- // MethodAttributes.Static | MethodAttributes.Public,
- // CallingConventions.Standard,
- // typeof (object), new Type[0], targetType, true);
- // var generator = method.GetILGenerator();
- // var methodInfo = targetType.GetMethod(methodName, BindingFlags.Instance);
- // generator.Emit(OpCodes.Ldarg_0);
- // generator.Emit(OpCodes.Castclass, targetType);
- // generator.Emit(OpCodes.Callvirt, methodInfo);
- // if (methodInfo.ReturnType == typeof(void)) {
- // generator.Emit(OpCodes.Ldnull);
- // }
- // else {
- // if (methodInfo.ReturnType.IsValueType) {
- // generator.Emit(OpCodes.Box, methodInfo.ReturnType);
- // }
- // }
- // generator.Emit(OpCodes.Ret);
- // return method.DelegateForCallMethod();
- //}
- #endregion
- #region Mod vs Not
- //-----------------------------
- //timer1: 00:00:02.4982942 (not)
- //timer2: 00:00:03.4878165 (mod)
- //-Total: 00:00:05.9861107
- //-based on 1.000.000.000 iterations
- //-----------------------------
- static void ModOrNot()
- {
- var iterations = 1000000000;
- var timer1 = ModVsNot_Not(iterations);
- var timer2 = ModVsNot_Mod(iterations);
- Console.WriteLine("-----------------------------");
- Console.WriteLine("timer1: " + timer1 + " (not)");
- Console.WriteLine("timer2: " + timer2 + " (mod)");
- Console.WriteLine("-Total: " + (timer1 + timer2));
- Console.WriteLine("-based on " + iterations + " iterations");
- Console.WriteLine("-----------------------------");
- }
- private static TimeSpan ModVsNot_Not(int iterations)
- {
- var timer = new Stopwatch();
- timer.Start();
- var oddRow = false;
- int x;
- for (var i = 0;i <= iterations;i++) {
- x = (oddRow ? 1 : 0);
- oddRow = !oddRow;
- }
- return timer.Elapsed;
- }
- private static TimeSpan ModVsNot_Mod(int iterations)
- {
- var timer = new Stopwatch();
- timer.Start();
- var sb = new StringBuilder();
- int x;
- for (var i = 0;i <= iterations;i++) {
- x = ((i % 2 == 0) ? 1 : 0);
- }
- return timer.Elapsed;
- }
- #endregion
- }
- public class Person
- {
- public int Id { get; set; }
- public string Name { get; set; }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment