<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>.Net Bits and Pieces &#187; Enum</title>
	<atom:link href="http://www.dotnetbitsandpieces.com/?feed=rss2&#038;tag=enum" rel="self" type="application/rss+xml" />
	<link>http://www.dotnetbitsandpieces.com</link>
	<description>What ever I find interesting to tell about .Net</description>
	<lastBuildDate>Sat, 06 Apr 2013 21:31:17 +0000</lastBuildDate>
	<language>en-US</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=3.9.40</generator>
	<item>
		<title>Enum.HasFlag() versus Bitwise comparison</title>
		<link>http://www.dotnetbitsandpieces.com/?p=11</link>
		<comments>http://www.dotnetbitsandpieces.com/?p=11#comments</comments>
		<pubDate>Mon, 14 Jan 2013 21:15:31 +0000</pubDate>
		<dc:creator><![CDATA[admin]]></dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[.Net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Enum]]></category>

		<guid isPermaLink="false">http://www.dotnetbitsandpieces.com/?p=11</guid>
		<description><![CDATA[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 [&#8230;]]]></description>
				<content:encoded><![CDATA[<p><span style="font-size: small;">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 </span><a href="http://geekswithblogs.net/BlackRabbitCoder/archive/2010/07/22/c-fundamentals-combining-enum-values-with-bit-flags.aspx" target="_blank"><span style="font-size: small;">this blog</span></a><span style="font-size: small;">. </span></p>
<p><span style="font-size: small;">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 </span><a href="http://www.codeproject.com/Tips/441086/NETs-Enum-HasFlag-and-performance-costs" target="_blank"><span style="font-size: small;">article</span></a><span style="font-size: small;">. 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.</span></p>
<p><span style="font-size: small;">I therefor did my own test to see what the performance overhead would be and if it was worth to change.<span id="more-11"></span></span></p>
<p><span style="font-size: small;">The first part was to create an enumeration</span></p>
<pre>

<div class="codecolorer-container text geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">public enum EnumValues<br />
{<br />
&nbsp; Value1 = 1,<br />
&nbsp; Value2 = 2,<br />
&nbsp; ...,<br />
&nbsp; Value16 = 32768<br />
}</div></div>

</pre>
<p><span style="font-size: small;">Next I needed a class to evaluate the different comparison methods</span></p>
<pre>

<div class="codecolorer-container text geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">public class EnumsClass<br />
{<br />
&nbsp; public bool CheckEnum(EnumValues values)<br />
&nbsp; {<br />
&nbsp; &nbsp; bool isTrue = false;<br />
&nbsp; &nbsp; if ((values &amp;amp; EnumValues.Value8) == EnumValues.Value8) isTrue = true;<br />
&nbsp; &nbsp; return isTrue;<br />
&nbsp; }<br />
<br />
&nbsp; public bool CheckEnumWithFlagMethod(EnumValues values)<br />
&nbsp; {<br />
&nbsp; &nbsp; bool isTrue = false;<br />
&nbsp; &nbsp; if (values.HasFlag(EnumValues.Value8)) isTrue = true;<br />
&nbsp; &nbsp; return isTrue;<br />
&nbsp; }<br />
}</div></div>

</pre>
<p><span style="font-size: small;">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.<br />
</span></p>
<pre>

<div class="codecolorer-container text geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">[TestClass]<br />
public class EnumTest<br />
{<br />
&nbsp; private EnumsClass _enumsClass;<br />
<br />
&nbsp; [TestInitialize]<br />
&nbsp; public void Initialize()<br />
&nbsp; {<br />
&nbsp; &nbsp; _enumsClass = new EnumsClass();<br />
&nbsp; }<br />
<br />
&nbsp; [TestMethod]<br />
&nbsp; public void TestForFalseValueWithFlag()<br />
&nbsp; {<br />
&nbsp; &nbsp; Assert.IsFalse(_enumsClass.CheckEnumWithFlagMethod(EnumValues.Value1 | EnumValues.Value10));<br />
&nbsp; }<br />
<br />
&nbsp; [TestMethod]<br />
&nbsp; public void TestForFalseValue()<br />
&nbsp; {<br />
&nbsp; &nbsp; Assert.IsFalse(_enumsClass.CheckEnum(EnumValues.Value1 | EnumValues.Value10));<br />
&nbsp; }<br />
<br />
&nbsp; [TestMethod]<br />
&nbsp; public void TestForTrueValue()<br />
&nbsp; {<br />
&nbsp; &nbsp; Assert.IsTrue(_enumsClass.CheckEnum(EnumValues.Value8 | EnumValues.Value10));<br />
&nbsp; }<br />
<br />
&nbsp; [TestMethod]<br />
&nbsp; public void TestForTrueValueWithFlag()<br />
&nbsp; {<br />
&nbsp; &nbsp; Assert.IsTrue(_enumsClass.CheckEnumWithFlagMethod(EnumValues.Value8 | EnumValues.Value10));<br />
&nbsp; }<br />
}</div></div>

</pre>
<p><span style="font-size: small;">To make sure that there was no difference between a true and false result I used both methods in the EnumsClass to return both <em>true</em> and <em>false. </em>I also repeated the different calls multiple times in a foreach loop. The results are in the below.</span></p>
<table width="400" border="0" cellspacing="0" cellpadding="2">
<tbody>
<tr>
<td valign="top" width="200">Test</td>
<td valign="top" width="200">1x</td>
<td valign="top" width="200">10000x</td>
<td valign="top" width="200">100000x</td>
<td valign="top" width="200">1000000x</td>
<td valign="top" width="200">10000000x</td>
</tr>
<tr>
<td valign="top" width="200">TestForTrueValue</td>
<td valign="top" width="200">&lt; 1 ms</td>
<td valign="top" width="200">&lt; 1 ms</td>
<td valign="top" width="200">&lt; 1 ms</td>
<td valign="top" width="200">7 ms</td>
<td valign="top" width="200">79 ms</td>
</tr>
<tr>
<td valign="top" width="200">TestForTrueValueWithFlag</td>
<td valign="top" width="200">&lt; 1 ms</td>
<td valign="top" width="200">&lt; 1 ms</td>
<td valign="top" width="200">4 ms</td>
<td valign="top" width="200">45 ms</td>
<td valign="top" width="200">453 ms</td>
</tr>
<tr>
<td valign="top" width="200">TestForFalseValue</td>
<td valign="top" width="200">&lt; 1 ms</td>
<td valign="top" width="200">&lt; 1 ms</td>
<td valign="top" width="200">&lt; 1 ms</td>
<td valign="top" width="200">6 ms</td>
<td valign="top" width="200">78 ms</td>
</tr>
<tr>
<td valign="top" width="200">TestForFalseValueWithFlag</td>
<td valign="top" width="200">&lt; 1 ms</td>
<td valign="top" width="200">&lt; 1 ms</td>
<td valign="top" width="200">5 ms</td>
<td valign="top" width="200">46 ms</td>
<td valign="top" width="200">477 ms</td>
</tr>
</tbody>
</table>
<p><span style="font-size: small;">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.</span><br />
<span style="font-size: small;">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.</span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.dotnetbitsandpieces.com/?feed=rss2&#038;p=11</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
