title: Performance of delegate and method group
body: <p>I was investigating the performance hit of creating <code>Cachedependency</code> objects, so I wrote a very simple test program as follows:</p>
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Diagnostics;
  using System.Web.Caching;
  
  namespace Test
  {
      internal class Program
      {
          private static readonly string[] keys = new[] {&quot;Abc&quot;};
          private static readonly int MaxIteration = 10000000;
  
          private static void Main(string[] args)
          {
              Debug.Print(&quot;first set&quot;);
              test2();
              test3();
              test4();
              test5();
              test6();
              test7();
              Debug.Print(&quot;second set&quot;);
              test7();
              test6();
              test5();
              test4();
              test3();
              test2();
          }
  
          private static void test2()
          {
              DateTime start = DateTime.Now;
              var list = new List&lt;CacheDependency&gt;();
              for (int i = 0; i &lt; MaxIteration; i++)
              {
                  list.Add(new CacheDependency(null, keys));
              }
  
              Debug.Print(&quot;test2 Time: &quot; + (DateTime.Now - start));
          }
  
          private static void test3()
          {
              DateTime start = DateTime.Now;
              var list = new List&lt;Func&lt;CacheDependency&gt;&gt;();
              for (int i = 0; i &lt; MaxIteration; i++)
              {
                  list.Add(() =&gt; new CacheDependency(null, keys));
              }
  
              Debug.Print(&quot;test3 Time: &quot; + (DateTime.Now - start));
          }
  
          private static void test4()
          {
              var p = new Program();
              DateTime start = DateTime.Now;
              var list = new List&lt;Func&lt;CacheDependency&gt;&gt;();
              for (int i = 0; i &lt; MaxIteration; i++)
              {
                  list.Add(p.GetDep);
              }
  
              Debug.Print(&quot;test4 Time: &quot; + (DateTime.Now - start));
          }
  
          private static void test5()
          {
              var p = new Program();
              DateTime start = DateTime.Now;
              var list = new List&lt;Func&lt;CacheDependency&gt;&gt;();
              for (int i = 0; i &lt; MaxIteration; i++)
              {
                  list.Add(() =&gt; { return p.GetDep(); });
              }
  
              Debug.Print(&quot;test5 Time: &quot; + (DateTime.Now - start));
          }
  
          private static void test6()
          {
              DateTime start = DateTime.Now;
              var list = new List&lt;Func&lt;CacheDependency&gt;&gt;();
              for (int i = 0; i &lt; MaxIteration; i++)
              {
                  list.Add(GetDepStatic);
              }
  
              Debug.Print(&quot;test6 Time: &quot; + (DateTime.Now - start));
          }
  
          private static void test7()
          {
              DateTime start = DateTime.Now;
              var list = new List&lt;Func&lt;CacheDependency&gt;&gt;();
              for (int i = 0; i &lt; MaxIteration; i++)
              {
                  list.Add(() =&gt; { return GetDepStatic(); });
              }
  
              Debug.Print(&quot;test7 Time: &quot; + (DateTime.Now - start));
          }
  
          private CacheDependency GetDep()
          {
              return new CacheDependency(null, keys);
          }
  
          private static CacheDependency GetDepStatic()
          {
              return new CacheDependency(null, keys);
          }
      }
  }
  </code></pre>
  <p>But I can't understand why these result looks like this:</p>
  <pre class="lang-none prettyprint-override"><code>first set
  test2 Time: 00:00:08.5394884
  test3 Time: 00:00:00.1820105
  test4 Time: 00:00:03.1401796
  test5 Time: 00:00:00.1910109
  test6 Time: 00:00:02.2041261
  test7 Time: 00:00:00.4840277
  second set
  test7 Time: 00:00:00.1850106
  test6 Time: 00:00:03.2941884
  test5 Time: 00:00:00.1750100
  test4 Time: 00:00:02.3561347
  test3 Time: 00:00:00.1830105
  test2 Time: 00:00:07.7324423
  
  </code></pre>
  <p>In particular:</p>
  <ol>
  <li>Why is <code>test4</code> and <code>test6</code> much slower
  than their delegate version? I also
  noticed that Resharper specifically
  has a comment on the delegate
  version suggesting change <code>test5</code> and
  <code>test7</code> to &quot;Convert to method group&quot;.
  Which is the same as <code>test4</code> and <code>test6</code>
  but they're actually slower?</li>
  <li>I don't seem a consistent
  performance difference when calling
  <code>test4</code> and <code>test6</code>, shouldn't static
  calls to be always faster?</li>
  </ol>