<?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>Strainu onLine &#187; C#</title>
	<atom:link href="http://www.strainu.ro/category/programming/c_sharp/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.strainu.ro</link>
	<description>Blogul unui automatist</description>
	<lastBuildDate>Fri, 28 Oct 2011 15:46:44 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.5</generator>
		<item>
		<title>What surprises can C# properties hide?</title>
		<link>http://www.strainu.ro/programming/c_sharp/what-surprises-can-c-properties-hide/</link>
		<comments>http://www.strainu.ro/programming/c_sharp/what-surprises-can-c-properties-hide/#comments</comments>
		<pubDate>Thu, 10 May 2007 08:39:41 +0000</pubDate>
		<dc:creator>Strainu</dc:creator>
				<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://blog.strainu.ro/personal/what-surprises-can-c-properties-hide/</guid>
		<description><![CDATA[One of my colleagues was recently working on an app that was supposed to transform a color bitmap into a black and white image. The image was quite large (1600&#215;1024) and the algorithm quite simple (2 nested for&#8217;s were used to go through the image). He had implemented this algorithm before, but with a small [...]]]></description>
			<content:encoded><![CDATA[<p>One of my colleagues was recently working on an app that was supposed to transform a color bitmap into a black and white image. The image was quite large (1600&#215;1024) and the algorithm quite simple (2 nested for&#8217;s were used to go through the image).</p>
<p>He had implemented this algorithm before, but with a small difference. Can you tell what the difference is?</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0600FF; font-weight: bold;">public</span> Bitmap Grayscale1<span style="color: #008000;">&#40;</span>Bitmap srcImg<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
<span style="color: #008000;">...</span><br />
<span style="color: #008080; font-style: italic;">// get source image size</span><br />
<span style="color: #6666cc; font-weight: bold;">int</span> width <span style="color: #008000;">=</span> srcImg<span style="color: #008000;">.</span><span style="color: #0000FF;">Width</span><span style="color: #008000;">;</span><br />
<span style="color: #6666cc; font-weight: bold;">int</span> height <span style="color: #008000;">=</span> srcImg<span style="color: #008000;">.</span><span style="color: #0000FF;">Height</span><span style="color: #008000;">;</span><br />
<span style="color: #008080; font-style: italic;">// for each line</span><br />
<span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> y <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> y <span style="color: #008000;">&amp;</span>lt<span style="color: #008000;">;</span> height<span style="color: #008000;">;</span> y<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
<span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> x <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> x <span style="color: #008000;">&amp;</span>lt<span style="color: #008000;">;</span> width<span style="color: #008000;">;</span> x<span style="color: #008000;">++</span>, src <span style="color: #008000;">+=</span> <span style="color: #FF0000;">3</span><span style="color: #008000;">&#41;</span><span style="color: #008080; font-style: italic;">// for each pixel</span><br />
<span style="color: #008000;">&#123;</span><br />
src<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> src<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">1</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> src<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">2</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">0.2125</span> <span style="color: #008000;">*</span> src<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">2</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">+</span> <span style="color: #FF0000;">0.7154</span> <span style="color: #008000;">*</span> src<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">1</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">+</span> <span style="color: #FF0000;">0.0721</span> <span style="color: #008000;">*</span> src<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<span style="color: #008000;">&#125;</span><br />
<span style="color: #008000;">...</span><br />
<span style="color: #008000;">&#125;</span><br />
<span style="color: #008000;">...</span><br />
<span style="color: #0600FF; font-weight: bold;">return</span> srcImg<span style="color: #008000;">;</span><br />
<span style="color: #008000;">&#125;</span></div></div>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0600FF; font-weight: bold;">public</span> Bitmap Grayscale2<span style="color: #008000;">&#40;</span>Bitmap srcImg<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
<span style="color: #008000;">...</span><br />
<span style="color: #008080; font-style: italic;">// for each line</span><br />
<span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> y <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> y <span style="color: #008000;">&amp;</span>lt<span style="color: #008000;">;</span> srcImg<span style="color: #008000;">.</span><span style="color: #0000FF;">Height</span><span style="color: #008000;">;</span> y<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
<span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> x <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> x <span style="color: #008000;">&amp;</span>lt<span style="color: #008000;">;</span> srcImg<span style="color: #008000;">.</span><span style="color: #0000FF;">Width</span><span style="color: #008000;">;</span> x<span style="color: #008000;">++</span>, src <span style="color: #008000;">+=</span> <span style="color: #FF0000;">3</span><span style="color: #008000;">&#41;</span><span style="color: #008080; font-style: italic;">// for each pixel</span><br />
<span style="color: #008000;">&#123;</span><br />
src<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> src<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">1</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> src<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">2</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">0.2125</span> <span style="color: #008000;">*</span> src<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">2</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">+</span> <span style="color: #FF0000;">0.7154</span> <span style="color: #008000;">*</span> src<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">1</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">+</span> <span style="color: #FF0000;">0.0721</span> <span style="color: #008000;">*</span> src<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<span style="color: #008000;">&#125;</span><br />
<span style="color: #008000;">...</span><br />
<span style="color: #008000;">&#125;</span><br />
<span style="color: #008000;">...</span><br />
<span style="color: #0600FF; font-weight: bold;">return</span> srcImg<span style="color: #008000;">;</span><br />
<span style="color: #008000;">&#125;</span></div></div>
<p>The answer: the second program was running much more slowly than the first. (almost 5 times slower, actually) <img src='http://www.strainu.ro/site/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>At first glance, the programs do the same thing. However, Grayscale1 was using two local variable, <em>width</em> and <em>height</em> to keep the size of the image. At each iteration, instead of calling the <em>Height</em> and <em>Width</em> properties of the image, the program was using the local variables.</p>
<p>So why was this causing such a dramatic effect on the running time? My friend&#8217;s idea was that the local variables were kept by the program in the registries, thus allowing for a quicker access time than the properties, which are in fact functions.</p>
<p>However, <a href="http://studentclub.ro/ovidiupl/" target="_blank">another programmer</a> decided to do a little bit of profiling on the program and discovered something much more interesting: the <em>getHeight()</em> and <em>getWidth()</em> functions (corresponding to the <em>Height</em> and <em>Width</em> properties of the image) were not just returning a variable, as one might expect, but were actually calling some GDI+ functions.</p>
<p>You can read <a href="http://studentclub.ro/victorparaschiv/archive/2006/10/07/O-surpriz_0301_-inedit_0301_.aspx" target="_blank">the original story</a>  and <a href="http://studentclub.ro/ovidiupl/archive/2006/10/08/Feri_1B02_i_2D00_v_0301_-de-surprize.aspx" target="_blank">the follow-up</a> (both in Romanian). And just in case you haven&#8217;t found out just yet, <a href="http://hardcode.ro/archive/2007/04/02/c-accessors.aspx" target="_blank">you COULD hit the same problem</a> in C++ (Microsoft C++, that is).</p>
<div class='sociable'><div><span class='sociable-tagline'><strong>Share and Enjoy:</strong></span></div><ul><li><a rel="nofollow" target="_blank"  href="http://www.facebook.com/share.php?u=http%3A%2F%2Fwww.strainu.ro%2Fprogramming%2Fc_sharp%2Fwhat-surprises-can-c-properties-hide%2F&amp;t=What%20surprises%20can%20C%23%20properties%20hide%3F" ><img src="http://www.strainu.ro/site/wp-content/plugins/sociable-30/images/default/16/facebook.png" class="sociable-img sociable-hovers" title="Facebook" alt="Facebook" /></a></li><li><a rel="nofollow" target="_blank"  href="http://twitter.com/home?status=What%20surprises%20can%20C%23%20properties%20hide%3F%20-%20http%3A%2F%2Fwww.strainu.ro%2Fprogramming%2Fc_sharp%2Fwhat-surprises-can-c-properties-hide%2F" ><img src="http://www.strainu.ro/site/wp-content/plugins/sociable-30/images/default/16/twitter.png" class="sociable-img sociable-hovers" title="Twitter" alt="Twitter" /></a></li><li><a rel="nofollow" target="_blank"  href="http://identi.ca/notice/new?status_textarea=http%3A%2F%2Fwww.strainu.ro%2Fprogramming%2Fc_sharp%2Fwhat-surprises-can-c-properties-hide%2F" ><img src="http://www.strainu.ro/site/wp-content/plugins/sociable-30/images/default/16/identica.png" class="sociable-img sociable-hovers" title="Identi.ca" alt="Identi.ca" /></a></li><li><a rel="nofollow" target="_blank"  href="mailto:?subject=What%20surprises%20can%20C%23%20properties%20hide%3F&amp;body=http%3A%2F%2Fwww.strainu.ro%2Fprogramming%2Fc_sharp%2Fwhat-surprises-can-c-properties-hide%2F" ><img src="http://www.strainu.ro/site/wp-content/plugins/sociable-30/images/default/16/email_link.png" class="sociable-img sociable-hovers" title="email" alt="email" /></a></li><li><a rel="nofollow" target="_blank" title="Add to favorites" href="#" onclick="AddToFavorites(); return false;"><img src="http://www.strainu.ro/site/wp-content/plugins/sociable-30/images/default/16/addtofavorites.png" class="sociable-img sociable-hovers" title="Add to favorites" alt="Add to favorites" /></a></li><li><a rel="nofollow" target="_blank"  href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.strainu.ro%2Fprogramming%2Fc_sharp%2Fwhat-surprises-can-c-properties-hide%2F&amp;title=What%20surprises%20can%20C%23%20properties%20hide%3F&amp;bodytext=One%20of%20my%20colleagues%20was%20recently%20working%20on%20an%20app%20that%20was%20supposed%20to%20transform%20a%20color%20bitmap%20into%20a%20black%20and%20white%20image.%20The%20image%20was%20quite%20large%20%281600x1024%29%20and%20the%20algorithm%20quite%20simple%20%282%20nested%20for%27s%20were%20used%20to%20go%20through%20the%20image%29.%0D%0A" ><img src="http://www.strainu.ro/site/wp-content/plugins/sociable-30/images/default/16/digg.png" class="sociable-img sociable-hovers" title="Digg" alt="Digg" /></a></li><li><a rel="nofollow" target="_blank"  href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.strainu.ro%2Fprogramming%2Fc_sharp%2Fwhat-surprises-can-c-properties-hide%2F&amp;title=What%20surprises%20can%20C%23%20properties%20hide%3F" ><img src="http://www.strainu.ro/site/wp-content/plugins/sociable-30/images/default/16/stumbleupon.png" class="sociable-img sociable-hovers" title="StumbleUpon" alt="StumbleUpon" /></a></li></ul></div>]]></content:encoded>
			<wfw:commentRss>http://www.strainu.ro/programming/c_sharp/what-surprises-can-c-properties-hide/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

