<?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>Ramunas Geciauskas &#187; programming</title>
	<atom:link href="http://geciauskas.com/category/programming/feed" rel="self" type="application/rss+xml" />
	<link>http://geciauskas.com</link>
	<description>Imagination is more important than knowledge</description>
	<lastBuildDate>Sun, 13 Jun 2010 06:02:13 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Good/Bad C# Programming Habits</title>
		<link>http://geciauskas.com/2010/programming/good-bad-c-programming-habits</link>
		<comments>http://geciauskas.com/2010/programming/good-bad-c-programming-habits#comments</comments>
		<pubDate>Mon, 01 Feb 2010 13:52:24 +0000</pubDate>
		<dc:creator>ramas</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[blog]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[habits]]></category>

		<guid isPermaLink="false">http://geciauskas.com/?p=35</guid>
		<description><![CDATA[Since I&#8217;m writing my Master&#8217;s degree thesis on topic related to design and analysis of .NET static code (CIL), I had a chance to take a deeper look in those C# code fragments which are compliant with Microsoft recommendations and those which are not, and compare their performance. Here is a short review of 3 [...]]]></description>
			<content:encoded><![CDATA[<p>Since I&#8217;m writing my Master&#8217;s degree thesis on topic related to design and analysis of .NET static code (CIL), I had a chance to take a deeper look in those C# code fragments which are compliant with Microsoft recommendations and those which are not, and compare their performance.</p>
<p>Here is a short review of 3 popular &#8220;bad&#8221; and &#8220;good&#8221; programming habits.</p>
<h3>&#8220;Bad&#8221; code:</h3>
<pre style="brush: php;">String res = String.Empty;
String app = "ABC";

for (int iCount = 0; iCount &lt; 1000; iCount++)
  res = res + app;

return res;</pre>
<h3>&#8220;Good&#8221; code:</h3>
<pre>String res = String.Empty;
String app = "ABC";

StringBuilder b = new StringBuilder();
for (int iCount = 0; iCount &lt; 1000; iCount++)
  b.Append(app);

return b.ToString();</pre>
<p><u>Result ["bad" vs "good"]:</u><br />
• Execution time (seconds): 1.63701196660469 vs 0.0429683102181981<br />
• Memory usage (bytes): 3,038,054 vs 33,532</p>
<p><a class="lightbox"  title ="String Speed" href="http://geciauskas.com/wp-content/uploads/2010/02/string_s.jpg"><img src="http://geciauskas.com/wp-content/uploads/2010/02/string_s-250x149.jpg" alt="" title="String Speed" width="250" height="149" class="alignleft size-medium wp-image-37" /></a><a class="lightbox"  title ="String Memory" href="http://geciauskas.com/wp-content/uploads/2010/02/string_m.jpg"><img src="http://geciauskas.com/wp-content/uploads/2010/02/string_m-250x149.jpg" alt="" title="String Memory" width="250" height="149" class="alignright size-medium wp-image-36" /></a></p>
<p>As we can see from the results you should always use StringBuilder class for string manipulation.</p>
<hr />
<h3>&#8220;Bad&#8221; code:</h3>
<pre>if (x.ToLower() == "geras")
</pre>
<h3>&#8220;Good&#8221; code:</h3>
<pre>if (String.Compare(x, "geras", true) == 0)
</pre>
<p><span style="text-decoration: underline;">Result ["bad" vs "good"]:</span></p>
<p>• Execution time (seconds): 0.198623009348954 vs 0.136355039537148</p>
<p>• Memory usage (bytes): 19,020 vs 19,596</p>
<p><a class="lightbox"  title ="Compare Speed" href="http://geciauskas.com/wp-content/uploads/2010/02/cmp_s.jpg"><img src="http://geciauskas.com/wp-content/uploads/2010/02/cmp_s-250x149.jpg" alt="" title="Compare Speed" width="250" height="149" class="alignleft size-medium wp-image-39" /></a><a class="lightbox"  title ="Compare Memory" href="http://geciauskas.com/wp-content/uploads/2010/02/cmp_m.jpg"><img src="http://geciauskas.com/wp-content/uploads/2010/02/cmp_m-250x149.jpg" alt="" title="Compare Memory" width="250" height="149" class="alignright size-medium wp-image-38" /></a></p>
<p>The increase of &#8220;good&#8221; code performance is apparent, but it will consume more RAM.</p>
<hr />
<h3>&#8220;Bad&#8221; code:</h3>
<pre>int i = 15;

int j = 25;</pre>
<p>Console.Write(&#8220;{0},{1}&#8221;, i, j);</p>
<h3>&#8220;Good&#8221; code:</h3>
<pre>int i = 15;

int j = 25;

Console.Write("{0},{1}", i.ToString(), j.ToString());
</pre>
<p><span style="text-decoration: underline;">Result ["bad" vs "good"]:</span></p>
<p>• Execution time (seconds): 0.00588817852548299 vs 0.0114441919294212</p>
<p>• Memory usage (bytes): 25,038 vs 24,934</p>
<p><a class="lightbox"  title ="Boxing Speed" href="http://geciauskas.com/wp-content/uploads/2010/02/box_s.jpg"><img src="http://geciauskas.com/wp-content/uploads/2010/02/box_s-250x149.jpg" alt="" title="Boxing Speed" width="250" height="149" class="alignleft size-medium wp-image-41" /></a><a class="lightbox"  title ="Boxing Memory" href="http://geciauskas.com/wp-content/uploads/2010/02/box_m.jpg"><img src="http://geciauskas.com/wp-content/uploads/2010/02/box_m-250x149.jpg" alt="" title="Boxing Memory" width="250" height="149" class="alignright size-medium wp-image-40" /></a></p>
<p>Even though the &#8220;good&#8221; code performs not as good as a &#8220;bad&#8221; code, but it will not require calling &#8220;box/unbox&#8221; operations, thus uses less memory.</p>
]]></content:encoded>
			<wfw:commentRss>http://geciauskas.com/2010/programming/good-bad-c-programming-habits/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Efficiency of Bitwise Operations in C#</title>
		<link>http://geciauskas.com/2010/programming/efficiency-of-bitwise-operations-in-c</link>
		<comments>http://geciauskas.com/2010/programming/efficiency-of-bitwise-operations-in-c#comments</comments>
		<pubDate>Mon, 01 Feb 2010 13:44:22 +0000</pubDate>
		<dc:creator>ramas</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[bitwise]]></category>
		<category><![CDATA[blog]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[performance]]></category>

		<guid isPermaLink="false">http://geciauskas.com/?p=29</guid>
		<description><![CDATA[Today I found great article in Polygonal.de blog about useful bitwise operations and attempted to determine how effective those &#8220;tricks&#8221; are in C# .NET. Note: - All test were performed 1000000000 times with same values of all variables - Difference was calculated using [(Method1_Time/Method2_Time)*100%] equation - >100% difference indicates that bitwise operation was faster Absolute [...]]]></description>
			<content:encoded><![CDATA[<p>Today I found great article in <a href="http://lab.polygonal.de/2007/05/10/bitwise-gems-fast-integer-math/">Polygonal.de</a> blog about useful bitwise operations and attempted to determine how effective those &#8220;tricks&#8221; are in C# .NET.</p>
<p><i>Note:<br />
- All test were performed 1000000000 times with same values of all variables</p>
<p>- Difference was calculated using [(Method1_Time/Method2_Time)*100%] equation<br />
- >100% difference indicates that bitwise operation was faster<br />
</i></p>
<p><strong>Absolute value of an integer</strong></p>
<pre>
x = i + (i >> 31) ^ (i >> 31);
versus
x = Math.Abs(x);

<i>
Basic method:   6.51467910027671
Bitwise method: 2.73749624603127

Difference:     238%
</i>
</pre>
<p>Note: This particular example only Works with 32 bit integers<br />
Universal code would look like this: [i + (i >> INT_SIZE-1) ^ (i >> INT_SIZE-1]</p>
<p><strong>Multiplication by any power of two</strong></p>
<pre>
x = x * 256;
versus
x = x << 8;

<i>
Basic method:   3.54274747209492
Bitwise method: 2.95297894006082

Difference:     120%
</i>
</pre>
<p><strong>Division by any power of two</strong></p>
<pre>
x = x / 256;
versus
x = x >> 8;

<i>
Basic method:   3.05552884514652
Bitwise method: 3.53027912765449

Difference:     87%
</i>
</pre>
<p>To my surprise, bitwise multiplication was faster, while division was slower (almost the same amount).</p>
<p><strong>Swapping integers without additional temporary variable</strong></p>
<pre>
int a = x;
x = y;
y = a;

versus

x ^= y;
y ^= x;
x ^= y;

<i>
Basic method:   4.45572132771064
Bitwise method: 4.04139101477981

Difference:     110%
</i>
</pre>
<p>My favorite bitwise operation, which I use quite often. Although it&#8217;s only 10% faster, but it doesn&#8217;t require creation of a new temporary integer, uses less memory therefore there&#8217;s no need calling .NET garbage collector at the end.</p>
<p><strong>Increase/Decrease integer by one</strong></p>
<pre>
x++;
x--;

versus

x = -~x;
x = ~-x;

<i>
Basic method:   3.44019505272318
Bitwise method: 3.70579836264106

Difference:     93%
</i>
</pre>
<p>Both x++ and x&#8211; are very frequent operations, so no wonder that compilers use all their potential to optimize them as good as they can. There&#8217;s no point using bitwise operations here.</p>
<p><strong>Flipping sign of an integer</strong></p>
<pre>
x = -x;
versus
x = ~x + 1;

<i>
Basic method:   3.07309755848858
Bitwise method: 3.05791937243421

Difference:     100%
</i>
</pre>
<p>Bitwise operation provides almost identical speed, so I don&#8217;t see why to complicate your code with bitwise analog of simple [x = -x] code.</p>
<p><strong>Modulo operation with divisors of any power by two</strong></p>
<pre>
x = x % 32;
versus
x = x &#038; (32 - 1);

<i>
Basic method:   2.65090814614707
Bitwise method: 3.55706940407231

Difference:     75%
</i>
</pre>
<p>The only bitwise operation that was a lot slower then simple [x % c] way.</p>
<p>Further reading:<br />
&bull; <a href="http://cmschill.net/stringtheory/2008/04/bitwise-operators/">cmschill.net</a><br />
&bull; <a href="http://avim.veneroida.com/?p=29">veneroida.com</a><br />
&bull; <a href="http://en.wikipedia.org/wiki/Bitwise_operation">wikipedia.org</a></p>
]]></content:encoded>
			<wfw:commentRss>http://geciauskas.com/2010/programming/efficiency-of-bitwise-operations-in-c/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
