giammin

Test object instance creation

May 3rd, 2012
522
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.20 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq.Expressions;
  5. using System.Reflection.Emit;
  6. using System.Runtime.Serialization;
  7.  
  8. namespace ConsoleApplication1
  9. {
  10.     public class Program
  11.     {
  12.         public static void Main()
  13.         {  
  14.             const int iterations = 10000;
  15.             var functions = new Dictionary<string, Func<Created>>();
  16.  
  17.             functions.Add("new", () => new Created());
  18.  
  19.             functions.Add("Activator", () => (Created)Activator.CreateInstance(typeof(Created)) );
  20.  
  21.             functions.Add("constructorInfo.Invoke", () =>
  22.                                     {
  23.                                         var constructorInfo = typeof(Created).GetConstructor(Type.EmptyTypes);
  24.                                         return (Created) constructorInfo.Invoke(null);
  25.                                     });
  26.             functions.Add("constructorInfo.Invoke GetUninitializedObject", () =>
  27.                                     {
  28.                                         var constructorInfo = typeof(Created).GetConstructor(Type.EmptyTypes);
  29.                                         object o = FormatterServices.GetUninitializedObject(typeof(Created));
  30.                                         return (Created)constructorInfo.Invoke(o, null);
  31.                                     });
  32.             functions.Add("DynamicMethod", () =>
  33.                                     {
  34.                                         var type = typeof (Created);
  35.                                         var constructorInfo = typeof(Created).GetConstructor(Type.EmptyTypes);
  36.                                         var dm = new DynamicMethod("MyCtor", type, Type.EmptyTypes, typeof(Program).Module, true);
  37.                                         ILGenerator ilgen = dm.GetILGenerator();
  38.                                         ilgen.Emit(OpCodes.Nop);
  39.                                         ilgen.Emit(OpCodes.Newobj, constructorInfo);
  40.                                         ilgen.Emit(OpCodes.Ret);
  41.  
  42.                                         return (Created)dm.Invoke(null, null);
  43.                                     });
  44.             functions.Add("DynamicMethod with Delegate", () =>
  45.                                     {  
  46.                                         var type = typeof (Created);
  47.                                         var constructorInfo = typeof(Created).GetConstructor(Type.EmptyTypes);
  48.                                         var dm = new DynamicMethod("MyCtor", type, Type.EmptyTypes, typeof(Program).Module, true);
  49.                                         ILGenerator ilgen = dm.GetILGenerator();
  50.                                         ilgen.Emit(OpCodes.Nop);
  51.                                         ilgen.Emit(OpCodes.Newobj, constructorInfo);
  52.                                         ilgen.Emit(OpCodes.Ret);
  53.                                         return ((Func<Created>)dm.CreateDelegate(typeof(Func<Created>)))();
  54.                                     });
  55.             functions.Add("Expression", () =>
  56.                                     {
  57.                                         var constructorInfo = typeof(Created).GetConstructor(Type.EmptyTypes);
  58.                                         var newExp = Expression.New(constructorInfo);
  59.                                         var lambda = Expression.Lambda(typeof(Func<Created>), newExp);
  60.                                         return ((Func<Created>) lambda.Compile())();
  61.                                     });
  62.  
  63.  
  64.             foreach (var item in functions)
  65.             {
  66.                 var watch = Stopwatch.StartNew();
  67.                 for (int i = 0; i < iterations; i++)
  68.                 {
  69.                    item.Value();
  70.  
  71.                 }
  72.                 Console.WriteLine("{1}  {0}",item.Key, watch.Elapsed);
  73.             }
  74.             Console.ReadLine();
  75.         }
  76.     }
  77.  
  78.     public class Created
  79.     {
  80.         public int Num;
  81.         public string Name;
  82.  
  83.         public Created(int num, string name)
  84.         {
  85.             Num = num;
  86.             Name = name;
  87.         }
  88.  
  89.         public Created()
  90.         {
  91.             Num = 1;
  92.             Name = "Created";
  93.         }
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment