title: C#: Virtual Function invocation is even faster than a delegate invocation?
body: <p>It just happens to me about one code design question. Say, I have one "template" method that invokes some functions that may "alter". A intuitive design is to follow "Template Design Pattern". Define the altering functions to be "virtual" functions to be overridden in subclasses. Or, I can just use delegate functions without "virtual". The delegate functions is injected so that they can be customized too. </p>
  
  <p>Originally, I thought the second "delegate" way would be faster than "virtual" way, but some coding snippet proves it is not correct.</p>
  
  <p>In below code, the first DoSomething method follows "template pattern". It calls on the virtual method IsTokenChar. The second DoSomthing method doesn't depend on virtual function. Instead, it has a pass-in delegate. In my computer, the first DoSomthing is always faster than the second. The result is like 1645:1780.</p>
  
  <p>"Virtual invocation" is dynamic binding and should be more time-costing than direct delegation invocation, right? but the result shows it is not.</p>
  
  <p>Anybody can explain this? </p>
  
  <pre><code>using System;
  using System.Diagnostics;
  
  class Foo
  {
      public virtual bool IsTokenChar(string word)
      {
          return String.IsNullOrEmpty(word);
      }
  
      // this is a template method
      public int DoSomething(string word)
      {
          int trueCount = 0;
          for (int i = 0; i &lt; repeat; ++i)
          {
              if (IsTokenChar(word))
              {
                  ++trueCount;
              }
          }
          return trueCount;
      }
  
      public int DoSomething(Predicate&lt;string&gt; predicator, string word)
      {
          int trueCount = 0;
          for (int i = 0; i &lt; repeat; ++i)
          {
              if (predicator(word))
              {
                  ++trueCount;
              }
          }
          return trueCount;
      }
  
      private int repeat = 200000000;
  }
  
  class Program
  {
      static void Main(string[] args)
      {
          Foo f = new Foo();
  
          {
              Stopwatch sw = Stopwatch.StartNew();
              f.DoSomething(null);
              sw.Stop();
              Console.WriteLine(sw.ElapsedMilliseconds);
          }
  
          {
              Stopwatch sw = Stopwatch.StartNew();
              f.DoSomething(str =&gt; String.IsNullOrEmpty(str), null);
              sw.Stop();
              Console.WriteLine(sw.ElapsedMilliseconds);
          }
      }
  }
  </code></pre>