Enum.HasFlag() versus Bitwise comparison

Call me a bit slow, but I recently discovered that Microsoft has implemented an HasFlag method on the Enum class since .Net 4. The method is meant as replacement for the bitwise comparison between values of an enumeration that has the FlagAttribute. For more information about the the FlagAttribute and bitwise comparison you can have a look at this blog.

For a project I am working on I am using the bitwise comparison extensively, so using the HasFlag method would make the code much more readable. Before using it I wanted to get some more information about how the HasFlag method works and I found this article. It shows the inner workings of the method, but also mentions that there is a performance cost to pay. However it does not mention what the cost is.

I therefor did my own test to see what the performance overhead would be and if it was worth to change.

The first part was to create an enumeration


public enum EnumValues
{
  Value1 = 1,
  Value2 = 2,
  ...,
  Value16 = 32768
}

Next I needed a class to evaluate the different comparison methods


public class EnumsClass
{
  public bool CheckEnum(EnumValues values)
  {
    bool isTrue = false;
    if ((values & EnumValues.Value8) == EnumValues.Value8) isTrue = true;
    return isTrue;
  }

  public bool CheckEnumWithFlagMethod(EnumValues values)
  {
    bool isTrue = false;
    if (values.HasFlag(EnumValues.Value8)) isTrue = true;
    return isTrue;
  }
}

The last part I needed was a class to call these two methods and determine how long each of the methods would take to run. I decided to use the unit test in Visual Studio 2012 as this also shows how long each test method takes.


[TestClass]
public class EnumTest
{
  private EnumsClass _enumsClass;

  [TestInitialize]
  public void Initialize()
  {
    _enumsClass = new EnumsClass();
  }

  [TestMethod]
  public void TestForFalseValueWithFlag()
  {
    Assert.IsFalse(_enumsClass.CheckEnumWithFlagMethod(EnumValues.Value1 | EnumValues.Value10));
  }

  [TestMethod]
  public void TestForFalseValue()
  {
    Assert.IsFalse(_enumsClass.CheckEnum(EnumValues.Value1 | EnumValues.Value10));
  }

  [TestMethod]
  public void TestForTrueValue()
  {
    Assert.IsTrue(_enumsClass.CheckEnum(EnumValues.Value8 | EnumValues.Value10));
  }

  [TestMethod]
  public void TestForTrueValueWithFlag()
  {
    Assert.IsTrue(_enumsClass.CheckEnumWithFlagMethod(EnumValues.Value8 | EnumValues.Value10));
  }
}

To make sure that there was no difference between a true and false result I used both methods in the EnumsClass to return both true and false. I also repeated the different calls multiple times in a foreach loop. The results are in the below.

Test 1x 10000x 100000x 1000000x 10000000x
TestForTrueValue < 1 ms < 1 ms < 1 ms 7 ms 79 ms
TestForTrueValueWithFlag < 1 ms < 1 ms 4 ms 45 ms 453 ms
TestForFalseValue < 1 ms < 1 ms < 1 ms 6 ms 78 ms
TestForFalseValueWithFlag < 1 ms < 1 ms 5 ms 46 ms 477 ms

So, there is an performance cost to using the HasFlag() method. It seems that the HasFlag() method is about 6 times as slow as the good old bitwise comparison. However you will notice the difference once you need to execute the call over 100000 times.
It is up to the programmer to see if the performance cost is worth the readability. I will stick to the bitwise comparison for now.

Leave a Reply

Your email address will not be published. Required fields are marked *

* Copy This Password *

* Type Or Paste Password Here *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>