title: Curiosity: Why does Expression<...> when compiled run faster than a minimal DynamicMethod?
body: <p>I'm currently doing some last-measure optimizations, mostly for fun and learning, and discovered something that left me with a couple of questions.</p>
  
  <p>First, the questions:</p>
  
  <ol>
  <li>When I construct a method in-memory through the use of <a href="http://msdn.microsoft.com/en-us/library/system.reflection.emit.dynamicmethod.aspx" rel="noreferrer">DynamicMethod</a>, and use the debugger, is there any way for me to step into the generated assembly code, when vieweing the code in the disassembler view? The debugger seems to just step over the whole method for me</li>
  <li>Or, if that's not possible, is it possible for me to somehow save the generated IL code to disk as an assembly, so that I can inspect it with <a href="http://www.red-gate.com/products/reflector/" rel="noreferrer">Reflector</a>?</li>
  <li>Why does the <code>Expression&lt;...&gt;</code> version of my simple addition method (Int32+Int32 => Int32) run faster than a minimal DynamicMethod version?</li>
  </ol>
  
  <p>Here's a short and complete program that demonstrates. On my system, the output is:</p>
  
  <pre><code>DynamicMethod: 887 ms
  Lambda: 1878 ms
  Method: 1969 ms
  Expression: 681 ms
  </code></pre>
  
  <p>I expected the lambda and method calls to have higher values, but the DynamicMethod version is consistently about 30-50% slower (variations probably due to Windows and other programs). Anyone know the reason?</p>
  
  <p>Here's the program:</p>
  
  <pre><code>using System;
  using System.Linq.Expressions;
  using System.Reflection.Emit;
  using System.Diagnostics;
  
  namespace Sandbox
  {
      public class Program
      {
          public static void Main(String[] args)
          {
              DynamicMethod method = new DynamicMethod("TestMethod",
                  typeof(Int32), new Type[] { typeof(Int32), typeof(Int32) });
              var il = method.GetILGenerator();
  
              il.Emit(OpCodes.Ldarg_0);
              il.Emit(OpCodes.Ldarg_1);
              il.Emit(OpCodes.Add);
              il.Emit(OpCodes.Ret);
  
              Func&lt;Int32, Int32, Int32&gt; f1 =
                  (Func&lt;Int32, Int32, Int32&gt;)method.CreateDelegate(
                      typeof(Func&lt;Int32, Int32, Int32&gt;));
              Func&lt;Int32, Int32, Int32&gt; f2 = (Int32 a, Int32 b) =&gt; a + b;
              Func&lt;Int32, Int32, Int32&gt; f3 = Sum;
              Expression&lt;Func&lt;Int32, Int32, Int32&gt;&gt; f4x = (a, b) =&gt; a + b;
              Func&lt;Int32, Int32, Int32&gt; f4 = f4x.Compile();
              for (Int32 pass = 1; pass &lt;= 2; pass++)
              {
                  // Pass 1 just runs all the code without writing out anything
                  // to avoid JIT overhead influencing the results
                  Time(f1, "DynamicMethod", pass);
                  Time(f2, "Lambda", pass);
                  Time(f3, "Method", pass);
                  Time(f4, "Expression", pass);
              }
          }
  
          private static void Time(Func&lt;Int32, Int32, Int32&gt; fn,
              String name, Int32 pass)
          {
              Stopwatch sw = new Stopwatch();
              sw.Start();
              for (Int32 index = 0; index &lt;= 100000000; index++)
              {
                  Int32 result = fn(index, 1);
              }
              sw.Stop();
              if (pass == 2)
                  Debug.WriteLine(name + ": " + sw.ElapsedMilliseconds + " ms");
          }
  
          private static Int32 Sum(Int32 a, Int32 b)
          {
              return a + b;
          }
      }
  }
  </code></pre>