title: Activator.CreateInstance<T> vs Compiled Expression. Inverse performance on two different machines
body: <p>A friend and I were testing using compiled expressions for object creation instead of <code>Activator.CreateInstance&lt;T&gt;</code> and ran into some interesting results. We found that when we ran the same code on each of our machines we saw completely opposite results. He got the expected result, significantly better performance out of the compiled expression, whereas I was surprised to see <code>Activator.CreateInstance&lt;T&gt;</code> out perform by 2x. </p>
  
  <p>Both computers ran compiled in .NET 4.0</p>
  
  <p>Computer 1 has .NET 4.5 installed. Computer 2 does not.</p>
  
  <p><strong>Computer 1 over 100000 objects:</strong></p>
  
  <pre><code>45ms - Type&lt;Test&gt;.New()
  19ms - System.Activator.CreateInstance&lt;Test&gt;();
  </code></pre>
  
  <p><strong>Computer 2 over 100000 objects:</strong></p>
  
  <pre><code>13ms - Type&lt;Test&gt;.New()
  86ms - System.Activator.CreateInstance&lt;Test&gt;();
  </code></pre>
  
  <p>And here is the code:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Diagnostics;
  using System.Linq;
  using System.Linq.Expressions;
  
  namespace NewNew
  {
      class Program
      {
          static void Main(string[] args)
          {
              Stopwatch benchmark = Stopwatch.StartNew();
              for (int i = 0; i &lt; 100000; i++)
              {
                  var result = Type&lt;Test&gt;.New();
              }
              benchmark.Stop();
              Console.WriteLine(benchmark.ElapsedMilliseconds + " Type&lt;Test&gt;.New()");
  
              benchmark = Stopwatch.StartNew();
              for (int i = 0; i &lt; 100000; i++)
              {
                  System.Activator.CreateInstance&lt;Test&gt;();
              }
              benchmark.Stop();
              Console.WriteLine(benchmark.ElapsedMilliseconds + " System.Activator.CreateInstance&lt;Test&gt;();");
              Console.Read();
          }
  
  
          static T Create&lt;T&gt;(params object[] args)
          {
              var types = args.Select(p =&gt; p.GetType()).ToArray();
              var ctor = typeof(T).GetConstructor(types);
  
              var exnew = Expression.New(ctor);
              var lambda = Expression.Lambda&lt;T&gt;(exnew);
              var compiled = lambda.Compile();
              return compiled;
          }
      }
  
      public delegate object ObjectActivator(params object[] args);
  
      public static class TypeExtensions
      {
          public static object New(this Type input, params object[] args)
          {
              if (TypeCache.Cache.ContainsKey(input))
                  return TypeCache.Cache[input](args);
  
              var types = args.Select(p =&gt; p.GetType());
              var constructor = input.GetConstructor(types.ToArray());
  
              var paraminfo = constructor.GetParameters();
  
              var paramex = Expression.Parameter(typeof(object[]), "args");
  
              var argex = new Expression[paraminfo.Length];
              for (int i = 0; i &lt; paraminfo.Length; i++)
              {
                  var index = Expression.Constant(i);
                  var paramType = paraminfo[i].ParameterType;
                  var accessor = Expression.ArrayIndex(paramex, index);
                  var cast = Expression.Convert(accessor, paramType);
                  argex[i] = cast;
              }
  
              var newex = Expression.New(constructor, argex);
              var lambda = Expression.Lambda(typeof(ObjectActivator), newex, paramex);
              var result = (ObjectActivator)lambda.Compile();
              TypeCache.Cache.Add(input, result);
              return result(args);
          }
      }
  
      public class TypeCache
      {
          internal static IDictionary&lt;Type, ObjectActivator&gt; Cache;
  
          static TypeCache()
          {
              Cache = new Dictionary&lt;Type, ObjectActivator&gt;();
          }
      }
  
      public class Type&lt;T&gt;
      {
          public static T New(params object[] args)
          {
              return (T)typeof(T).New(args);
          }
      }
  
      public class Test
      {
          public Test()
          {
  
          }
  
          public Test(string name)
          {
              Name = name;
          }
  
          public string Name { get; set; }
      }
  }
  </code></pre>