<?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; habits</title>
	<atom:link href="http://geciauskas.com/tag/habits/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>
	</channel>
</rss>
