title: What is it that makes Enum.HasFlag so slow?
body: <p>I was doing some speed tests and I noticed that Enum.HasFlag is about 16 times slower than using the bitwise operation.</p>
  <p>Does anyone know the internals of Enum.HasFlag and why it is so slow? I mean twice as slow wouldn't be too bad but it makes the function unusable when its 16 times slower.</p>
  <p>In case anyone is wondering, here is the code I am using to test its speed.</p>
  <pre class="lang-cs prettyprint-override"><code>using System;
  using System.Collections.Generic;
  using System.Diagnostics;
  using System.Linq;
  
  namespace app
  {
      public class Program
      {
          [Flags]
          public enum Test
          {
              Flag1 = 1,
              Flag2 = 2,
              Flag3 = 4,
              Flag4 = 8
          }
          static int num = 0;
          static Random rand;
          static void Main(string[] args)
          {
              int seed = (int)DateTime.UtcNow.Ticks;
  
              var st1 = new SpeedTest(delegate
              {
                  Test t = Test.Flag1;
                  t |= (Test)rand.Next(1, 9);
                  if (t.HasFlag(Test.Flag4))
                      num++;
              });
  
              var st2 = new SpeedTest(delegate
              {
                  Test t = Test.Flag1;
                  t |= (Test)rand.Next(1, 9);
                  if (HasFlag(t , Test.Flag4))
                      num++;
              });
  
              rand = new Random(seed);
              st1.Test();
              rand = new Random(seed);
              st2.Test();
  
              Console.WriteLine(&quot;Random to prevent optimizing out things {0}&quot;, num);
              Console.WriteLine(&quot;HasFlag: {0}ms {1}ms {2}ms&quot;, st1.Min, st1.Average, st1.Max);
              Console.WriteLine(&quot;Bitwise: {0}ms {1}ms {2}ms&quot;, st2.Min, st2.Average, st2.Max);
              Console.ReadLine();
          }
          static bool HasFlag(Test flags, Test flag)
          {
              return (flags &amp; flag) != 0;
          }
      }
      [DebuggerDisplay(&quot;Average = {Average}&quot;)]
      class SpeedTest
      {
          public int Iterations { get; set; }
  
          public int Times { get; set; }
  
          public List&lt;Stopwatch&gt; Watches { get; set; }
  
          public Action Function { get; set; }
  
          public long Min { get { return Watches.Min(s =&gt; s.ElapsedMilliseconds); } }
  
          public long Max { get { return Watches.Max(s =&gt; s.ElapsedMilliseconds); } }
  
          public double Average { get { return Watches.Average(s =&gt; s.ElapsedMilliseconds); } }
  
          public SpeedTest(Action func)
          {
              Times = 10;
              Iterations = 100000;
              Function = func;
              Watches = new List&lt;Stopwatch&gt;();
          }
  
          public void Test()
          {
              Watches.Clear();
              for (int i = 0; i &lt; Times; i++)
              {
                  var sw = Stopwatch.StartNew();
                  for (int o = 0; o &lt; Iterations; o++)
                  {
                      Function();
                  }
                  sw.Stop();
                  Watches.Add(sw);
              }
          }
      }
  }
  </code></pre>
  <p>Results:</p>
  <pre><code>HasFlag: 52ms 53.6ms 55ms
  Bitwise: 3ms 3ms 3ms
  </code></pre>