<?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 Tips and Tricks &#187; Optimization</title>
	<atom:link href="http://kossovsky.net/index.php/category/optimization/feed/" rel="self" type="application/rss+xml" />
	<link>http://kossovsky.net</link>
	<description>C# Code Snippets, ASP.NET Code Samples, .NET Tips and Tricks</description>
	<lastBuildDate>Sat, 25 Dec 2010 08:32:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>C# Set method timeout using Generics</title>
		<link>http://kossovsky.net/index.php/2009/07/csharp-how-to-limit-method-execution-time/</link>
		<comments>http://kossovsky.net/index.php/2009/07/csharp-how-to-limit-method-execution-time/#comments</comments>
		<pubDate>Wed, 15 Jul 2009 18:30:17 +0000</pubDate>
		<dc:creator>Xander</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Optimization]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[asynchronous]]></category>
		<category><![CDATA[c#.net]]></category>
		<category><![CDATA[c-sharp]]></category>
		<category><![CDATA[execution]]></category>
		<category><![CDATA[file ext]]></category>
		<category><![CDATA[func]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[generics]]></category>
		<category><![CDATA[httpwebrequest]]></category>
		<category><![CDATA[ins]]></category>
		<category><![CDATA[limit]]></category>
		<category><![CDATA[lt]]></category>
		<category><![CDATA[milliseconds]]></category>
		<category><![CDATA[net c#]]></category>
		<category><![CDATA[network]]></category>
		<category><![CDATA[quick solution]]></category>
		<category><![CDATA[remote computer]]></category>
		<category><![CDATA[return result]]></category>
		<category><![CDATA[synchronous]]></category>
		<category><![CDATA[thread]]></category>
		<category><![CDATA[time]]></category>
		<category><![CDATA[timeout]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[unc]]></category>
		<category><![CDATA[unc path]]></category>
		<category><![CDATA[visual c#]]></category>

		<guid isPermaLink="false">http://kossovsky.net/?p=687</guid>
		<description><![CDATA[I&#8217;m pretty sure all of you know the WebRequest and it&#8217;s derived class HttpWebRequest. And what a marvelous property both of them have &#8211; the TimeOut. Yesterday I had to write some app that reads files located on some remote computer. As I knew already this ins&#8217;t such a good practice, because your code can [...]<br /><div><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx.php?value=4.6" /></div><div>Rating: 4.6/<strong>5</strong> (18 votes cast)</div><br /><a target="_blank" href="http://www.gdstarrating.com/"><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx/powered.png" border="0" width="80" height="15" /></a><br />]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m pretty sure all of you know the WebRequest and it&#8217;s derived class HttpWebRequest.<br />
And what a marvelous property both of them have &#8211; the TimeOut.</p>
<p><span id="more-687"></span></p>
<p>Yesterday I had to write some app that reads files located on some remote computer.<br />
As I knew already this ins&#8217;t such a good practice, because your code can just hang/freeze for seconds waiting for that UNC path to become available or just checking it&#8217;s existence.</p>
<p>Still, I had to provide some quick solution and &#8220;hoping for the best&#8221; wasn&#8217;t good enough.<br />
So, this is what I came up with.</p>
<pre class="c-sharp" name="code">
        public static T Limex&lt;T&gt;(Func&lt;T&gt; F, int Timeout, out bool Completed)
        {
            T result = default(T);
            Thread thread = new Thread(() =&gt; result = F());
            thread.Start();
            Completed = thread.Join(Timeout);
            if (!Completed) thread.Abort();
            return result;
        }

        // Overloaded method, for cases when we don't
        // need to know if the method was terminated
        public static T Limex&lt;T&gt;(Func&lt;T&gt; F, int Timeout)
        {
            bool Completed;
            return Limex(F, Timeout, out Completed);
        }
</pre>
<p>The usage is very simple, just pass any method (declared or anonymous) and the desired Timeout in milliseconds to the Limex. </p>
<p>Example :</p>
<pre class="c-sharp" name="code">
bool Completed;
string Content = Limex(() => File.ReadAllText(@"\\unc\dir\file.ext")
                       ,100 // milliseconds
                       ,out Completed);

if (Completed)
   // Do something
else
  // Do something else
</pre>
<p>Comments and suggestions for improvement are welcome and will be gratefully appreciated</p>
<br /><div><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx.php?value=4.6" /></div><div>Rating: 4.6/<strong>5</strong> (18 votes cast)</div><br /><a target="_blank" href="http://www.gdstarrating.com/"><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx/powered.png" border="0" width="80" height="15" /></a><br />
<p class="FacebookLikeButton"><fb:like href="http%3A%2F%2Fkossovsky.net%2Findex.php%2F2009%2F07%2Fcsharp-how-to-limit-method-execution-time%2F" layout="standard" show_faces="false" width="450" action="like" colorscheme="light"></fb:like></p>
]]></content:encoded>
			<wfw:commentRss>http://kossovsky.net/index.php/2009/07/csharp-how-to-limit-method-execution-time/feed/</wfw:commentRss>
		<slash:comments>23</slash:comments>
		</item>
		<item>
		<title>Javascript pseudo protocol (is EVIL)</title>
		<link>http://kossovsky.net/index.php/2009/07/javascript-pseudo-protocol-is-evil/</link>
		<comments>http://kossovsky.net/index.php/2009/07/javascript-pseudo-protocol-is-evil/#comments</comments>
		<pubDate>Sun, 12 Jul 2009 18:35:42 +0000</pubDate>
		<dc:creator>Maxim</dc:creator>
				<category><![CDATA[HTML]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Optimization]]></category>
		<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[UI]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[href javascript]]></category>
		<category><![CDATA[javascript problems]]></category>
		<category><![CDATA[JS]]></category>
		<category><![CDATA[script type]]></category>
		<category><![CDATA[span style]]></category>
		<category><![CDATA[style color]]></category>
		<category><![CDATA[style text]]></category>
		<category><![CDATA[text decoration]]></category>
		<category><![CDATA[text element]]></category>
		<category><![CDATA[text javascript]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[type javascript]]></category>

		<guid isPermaLink="false">http://kossovsky.net/?p=586</guid>
		<description><![CDATA[This post is somehow related to my previous post about javascript function context.  Recently I helped one of my friends to debug a very funny problem. He told me that his inline event handler doen&#8217;t work, because he could not pass this object to his function. You probably wonder what did I find? &#60;script type="text/javascript"&#62; function [...]<br /><div><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx.php?value=4.6" /></div><div>Rating: 4.6/<strong>5</strong> (5 votes cast)</div><br /><a target="_blank" href="http://www.gdstarrating.com/"><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx/powered.png" border="0" width="80" height="15" /></a><br />]]></description>
			<content:encoded><![CDATA[<p>This post is somehow related to my previous post about <a href="http://kossovsky.net/index.php/2009/07/function-context-and-apply-function/" target="_blank">javascript function context</a>.  Recently I helped one of my friends to debug a very funny problem. He told me that his inline event handler doen&#8217;t work, because he could not pass <strong><em>this</em></strong> object to his function.<br />
You probably wonder what did I find? <img src='http://kossovsky.net/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /><br />
<span id="more-586"></span></p>
<pre class="javascript" name="code">    &lt;script type="text/javascript"&gt;
        function doSomething(obj) {
            obj.style.color = '#cc0000';
        }
    &lt;/script&gt;
    &lt;a href="javascript:doSomething(this)"&gt;Link&lt;/a&gt;</pre>
<p><strong><em>this</em></strong> keyword in his case was not A object as expected but <strong><em>window</em></strong>.<br />
Having javascript:  in href attribute is like running JavaScript from address bar of your browser. It will run in <strong><em>window</em></strong> context.<br />
Only if my friend was writing his code like this:</p>
<pre class="javascript" name="code">   ...
    &lt;a onclick="doSomething(this)"&gt;Link&lt;/a&gt;</pre>
<p>it would work like charm.</p>
<p>But I never use <strong><em>javascript:</em></strong> at all in my markup. Why?<br />
1. If you need text element that is clickable use span, any visual styling like text-decoration or cursor can be achieved using CSS.<br />
2. If you don&#8217;t have actual link behind you shouldn&#8217;t use A tag. As far as I know it&#8217;s not good for SEO.<br />
3. Javascript problems as i described above can arise<br />
I would write the code above like this:</p>
<pre class="javascript" name="code">    ...
    &lt;span style="text-decoration:underline;cursor:pointer;"
                 onclick="doSomething(this)"&gt;Link&lt;/span&gt;</pre>
<p>The only reason I know to use A for JavaScript  triggers is to achieve hover effect without use of JavaScript.<br />
If you know another reason why you should/shouldn&#8217;t use links as JavaScript triggers you&#8217;re welcome to comment.<br />
After this post you can get an impression that I don&#8217;t like javascript: notation and you will be wrong,  actually there are very usefull tricks I use all the time:<br />
1. Check some JS value on your page. Type javascript:alert(yourvariable) in the address bar and press enter.<br />
<img src="http://kossovsky.net/wp-content/uploads/2009/07/1.jpg" alt="" width="299" height="35" /></p>
<p>You will get :<br />
<img src="http://kossovsky.net/wp-content/uploads/2009/07/2.jpg" alt="" width="187" height="134" /></p>
<p>2. Or let&#8217;s say you have function somewhere on the page and you have to see it quick without searching through all your scripts. Or you wan&#8217;t to <span style="text-decoration: line-through">steal</span> see how someone implemented something <img src='http://kossovsky.net/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /><br />
Just type  javascript:alert(yourdesiredfunction) and voila:<br />
<img src="http://kossovsky.net/wp-content/uploads/2009/07/3.jpg" alt="" width="283" height="44" /></p>
<p><img src="http://kossovsky.net/wp-content/uploads/2009/07/4.jpg" alt="" width="260" height="140" /></p>
<p>UPD:<br />
Just googled good page with JS best practices, it has Javascript: pseudoprotocol part that says almost exactly the same as my conclusions.<br />
<a href="http://www.javascripttoolbox.com/bestpractices/">http://www.javascripttoolbox.com/bestpractices/</a></p>
<br /><div><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx.php?value=4.6" /></div><div>Rating: 4.6/<strong>5</strong> (5 votes cast)</div><br /><a target="_blank" href="http://www.gdstarrating.com/"><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx/powered.png" border="0" width="80" height="15" /></a><br />
<p class="FacebookLikeButton"><fb:like href="http%3A%2F%2Fkossovsky.net%2Findex.php%2F2009%2F07%2Fjavascript-pseudo-protocol-is-evil%2F" layout="standard" show_faces="false" width="450" action="like" colorscheme="light"></fb:like></p>
]]></content:encoded>
			<wfw:commentRss>http://kossovsky.net/index.php/2009/07/javascript-pseudo-protocol-is-evil/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>C# Rectangle Packing</title>
		<link>http://kossovsky.net/index.php/2009/07/cshar-rectangle-packing/</link>
		<comments>http://kossovsky.net/index.php/2009/07/cshar-rectangle-packing/#comments</comments>
		<pubDate>Thu, 09 Jul 2009 12:45:48 +0000</pubDate>
		<dc:creator>Xander</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Graphics]]></category>
		<category><![CDATA[Optimization]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[ascii character]]></category>
		<category><![CDATA[bitmap fonts]]></category>
		<category><![CDATA[c#.net]]></category>
		<category><![CDATA[c-sharp]]></category>
		<category><![CDATA[common interface]]></category>
		<category><![CDATA[cygon]]></category>
		<category><![CDATA[Gif]]></category>
		<category><![CDATA[humble webmaster]]></category>
		<category><![CDATA[lightmaps]]></category>
		<category><![CDATA[linear increase]]></category>
		<category><![CDATA[net c#]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[Png]]></category>
		<category><![CDATA[Puzzle]]></category>
		<category><![CDATA[space efficiency]]></category>
		<category><![CDATA[Speed]]></category>
		<category><![CDATA[suitable position]]></category>
		<category><![CDATA[support assembly]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[visual c#]]></category>

		<guid isPermaLink="false">http://kossovsky.net/?p=414</guid>
		<description><![CDATA[Sometimes, you&#8217;re faced with the problem to cramp as many smaller textures as possible onto a larger texture. Typical cases are lightmaps, which are very small textures with dimensions that usually are not powers of two, or bitmap fonts where you want to try and fit the entire ascii character set onto a texture without [...]<br /><div><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx.php?value=4.3" /></div><div>Rating: 4.3/<strong>5</strong> (6 votes cast)</div><br /><a target="_blank" href="http://www.gdstarrating.com/"><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx/powered.png" border="0" width="80" height="15" /></a><br />]]></description>
			<content:encoded><![CDATA[<p>Sometimes, you&#8217;re faced with the problem to cramp as many smaller textures as possible onto a larger texture. Typical cases are lightmaps, which are very small textures with dimensions that usually are not powers of two, or bitmap fonts where you want to try and fit the entire ascii character set onto a texture without wasting much space.</p>
<p>What you need then, is a rectangle packer, an algorithm that arranges as many smaller rectangles on a larger rectangle as can possibly fit.</p>
<p><span id="more-414"></span></p>
<p>The <a title="Nuclex - Rectangle Packing" href="http://www.nuclex.org/pages/framework/rectangle-packing" target="_blank">Nuclex.Support</a> assembly contains several rectangle packing algorithms in the Nuclex.Support.Packing namespace offering various levels of compromises between space efficiency and runtime performance. All of these algorithms have been implemented as classes with a common interface, so, once implemented, you can easily switch forth and back between different algorithms to find the one best suites for your purpose.</p>
<p>Currently, there are three different packing algorithms for you to choose:</p>
<table style="width: 100%; border: none;" border="0" cellspacing="2" cellpadding="0">
<tbody>
<tr>
<td style="border:none;padding-bottom:20px;" align="center" valign="top"><img style="padding: 0px; margin: 0px; border: none;" title="The Simple Packer" src="http://kossovsky.net/wp-content/uploads/2009/07/simple-rectangle-packer.png" alt="The Simple Packer" width="128" height="128" /></td>
<td style="border:none;padding-bottom:20px;" valign="top"><strong>The Simple Packer</strong><br />
This packer is optimized for runtime performance. It does sacrifice a bit of space, but the time needed to find a position for a new rectangle is O(1). In english, that means it doesn&#8217;t take longer to search for a suitable position, no matter how many rectangles have been added to the packing area.</td>
</tr>
<tr>
<td style="border:none;padding-bottom:20px;" align="center" valign="top"><img style="padding: 0px; margin: 0px; border: none;" title="cygon-rectangle-packer" src="http://kossovsky.net/wp-content/uploads/2009/07/cygon-rectangle-packer.png" alt="The Cygon Packer" width="128" height="128" /></td>
<td style="border:none;padding-bottom:20px;" valign="top"><strong>The Cygon Packer</strong><br />
Named after your humble webmaster, this algorithm is highly efficient in its space usage and offers reasonable performance. It will never exceed O(n) time but generally achieves almost O(1) on average. English translation: Search time is usually constant and guaranteed to never exceed a linear increase (so when you&#8217;ve got 99 rectangles already packed and add the 100th one, it guarantees that search time will at most be 1% longer than the time taken for the previous rectangle).</td>
</tr>
<tr>
<td style="border:none;padding-bottom:20px;" align="center" valign="top"><img style="padding: 0px; margin: 0px; border: none;" title="arevalo-rectangle-packer" src="http://kossovsky.net/wp-content/uploads/2009/07/arevalo-rectangle-packer.png" alt="The Arevalo Packer" width="128" height="128" /></td>
<td style="border:none;padding-bottom:20px;" valign="top"><strong>The Arevalo Packer</strong><br />
Named after Javier Arevalo, who posted his implementation of this packer on flipcode back in the golden times. This algorithm offers very good space efficiency and runs in slightly less then O(n) time. Just to be consistent, in english, that means, the time needed to find a suitable place for a new rectangle will only increase linearly.</td>
</tr>
</tbody>
</table>
<p>You can download the C# version of the algorithms from <a title="Nuclex Framework - C# Source Code" href="https://devel.nuclex.org/framework/browser/game/Nuclex.Game/trunk/Source/Packing" target="_blank">here</a>.</p>
<p>The usage is very simple :</p>
<pre class="c-sharp" name="code">
	Point PP;
	var ARP = new ArevaloRectanglePacker(200, 200);

	if(!ARP.TryPack(Width, Height, out PP)) {
		// Do something
	}
	else {
		// Do something else
	}
</pre>
<br /><div><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx.php?value=4.3" /></div><div>Rating: 4.3/<strong>5</strong> (6 votes cast)</div><br /><a target="_blank" href="http://www.gdstarrating.com/"><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx/powered.png" border="0" width="80" height="15" /></a><br />
<p class="FacebookLikeButton"><fb:like href="http%3A%2F%2Fkossovsky.net%2Findex.php%2F2009%2F07%2Fcshar-rectangle-packing%2F" layout="standard" show_faces="false" width="450" action="like" colorscheme="light"></fb:like></p>
]]></content:encoded>
			<wfw:commentRss>http://kossovsky.net/index.php/2009/07/cshar-rectangle-packing/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>SpeedTrace &#8211; .NET Profiler and Tracer</title>
		<link>http://kossovsky.net/index.php/2009/07/speedtrace-net-profiler-and-tracer/</link>
		<comments>http://kossovsky.net/index.php/2009/07/speedtrace-net-profiler-and-tracer/#comments</comments>
		<pubDate>Thu, 09 Jul 2009 11:28:30 +0000</pubDate>
		<dc:creator>Xander</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Optimization]]></category>
		<category><![CDATA[Profiling Tools]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[QA]]></category>
		<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[bug]]></category>
		<category><![CDATA[bugs]]></category>
		<category><![CDATA[c#.net]]></category>
		<category><![CDATA[c-sharp]]></category>
		<category><![CDATA[data flow]]></category>
		<category><![CDATA[dot net framework]]></category>
		<category><![CDATA[dot net framework 2]]></category>
		<category><![CDATA[flow problems]]></category>
		<category><![CDATA[net c#]]></category>
		<category><![CDATA[peak times]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[performance bottlenecks]]></category>
		<category><![CDATA[profiler]]></category>
		<category><![CDATA[profilers]]></category>
		<category><![CDATA[profiling]]></category>
		<category><![CDATA[program flow]]></category>
		<category><![CDATA[return values]]></category>
		<category><![CDATA[Speed]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[visual c#]]></category>

		<guid isPermaLink="false">http://kossovsky.net/?p=406</guid>
		<description><![CDATA[It is very important to be able to dynamically control the behavior of .NET applications and to keep track of some of the aspects of the application (i.e. how the application is performing, what errors are produced during runtime, how the application performs at peak times, how to dynamically alter the behavior of the application, [...]<br /><div><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx.php?value=4.7" /></div><div>Rating: 4.7/<strong>5</strong> (7 votes cast)</div><br /><a target="_blank" href="http://www.gdstarrating.com/"><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx/powered.png" border="0" width="80" height="15" /></a><br />]]></description>
			<content:encoded><![CDATA[<p>It is very important to be able to dynamically control the behavior of .NET applications and to keep track of some of the aspects of the application (i.e. how the application is performing, what errors are produced during runtime, how the application performs at peak times, how to dynamically alter the behavior of the application, etc).</p>
<p><span id="more-406"></span></p>
<p>The <a title="SpeedTrace - .NET Profiler and Tracer" href="http://www.ipcas.com/trace-and-profile/c-sharp-and-vb.net-tracer-and-profiler/" target="_blank">SpeedTrace Pro</a> profiler follows and records the program flow of any .NET application on a function/method level for later profiling or tracing analysis, allowing you to identify performance bottlenecks, deadlocks, software design problems as well as resource and data flow problems.</p>
<p>One of the nicest features in this application is the presence of  &#8220;Callback-API&#8221;.<br />
It provides user-extended functionality to allow developers to adapt the tracer to their needs and to write user-specific extensions for dot.NET Framework 2.0, 3.0 or 3.5 applications.</p>
<p>SpeedTrace Pro can make accessible the arguments and return values of a function. This outstanding feature enables you not only to trace function calls but also to obtain the values passed or returned &#8211; important data that normal profilers lose during aggregations. You can make use of this data to obtain the relevant information, especially when bugs appear under certain conditions.</p>
<p>The data tracing values of .NET applications may be used to trap bugs easily and to perform data-dependent analyses.</p>
<div id="attachment_408" class="wp-caption alignnone" style="width: 310px"><img class="size-medium wp-image-408" title="dotnet-tracer-and-profiler" src="http://kossovsky.net/wp-content/uploads/2009/07/dotnet-tracer-and-profiler-300x224.png" alt="SpeedTrace - .NET Profiler and Tracer" width="300" height="224" /><p class="wp-caption-text">SpeedTrace - .NET Profiler and Tracer</p></div>
<br /><div><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx.php?value=4.7" /></div><div>Rating: 4.7/<strong>5</strong> (7 votes cast)</div><br /><a target="_blank" href="http://www.gdstarrating.com/"><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx/powered.png" border="0" width="80" height="15" /></a><br />
<p class="FacebookLikeButton"><fb:like href="http%3A%2F%2Fkossovsky.net%2Findex.php%2F2009%2F07%2Fspeedtrace-net-profiler-and-tracer%2F" layout="standard" show_faces="false" width="450" action="like" colorscheme="light"></fb:like></p>
]]></content:encoded>
			<wfw:commentRss>http://kossovsky.net/index.php/2009/07/speedtrace-net-profiler-and-tracer/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>C# The &#8220;double&#8221; trouble</title>
		<link>http://kossovsky.net/index.php/2009/07/csharp-the-double-trouble/</link>
		<comments>http://kossovsky.net/index.php/2009/07/csharp-the-double-trouble/#comments</comments>
		<pubDate>Mon, 06 Jul 2009 21:37:16 +0000</pubDate>
		<dc:creator>Xander</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Interview Questions]]></category>
		<category><![CDATA[Optimization]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[QA]]></category>
		<category><![CDATA[Teasers]]></category>
		<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[bug]]></category>
		<category><![CDATA[bugs]]></category>
		<category><![CDATA[c#.net]]></category>
		<category><![CDATA[c-sharp]]></category>
		<category><![CDATA[d1 d2]]></category>
		<category><![CDATA[digits]]></category>
		<category><![CDATA[double]]></category>
		<category><![CDATA[double trouble]]></category>
		<category><![CDATA[floating]]></category>
		<category><![CDATA[floating point]]></category>
		<category><![CDATA[guarantees]]></category>
		<category><![CDATA[interview]]></category>
		<category><![CDATA[main string]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[net c#]]></category>
		<category><![CDATA[numeric value]]></category>
		<category><![CDATA[point]]></category>
		<category><![CDATA[precision]]></category>
		<category><![CDATA[Puzzle]]></category>
		<category><![CDATA[simple answer]]></category>
		<category><![CDATA[string args]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Tricks]]></category>
		<category><![CDATA[visual c#]]></category>

		<guid isPermaLink="false">http://kossovsky.net/?p=360</guid>
		<description><![CDATA[This is a very simple question with a not so simple answer&#8230; Forget for a second about .NET. If someone asks you how much is 1.000025 &#8211; 0.000025 your answer will probably be &#8220;1&#8243; and correct. An easy question, right ? Now, let&#8217;s go back to .NET and check if our calculations are correct. static [...]<br /><div><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx.php?value=4.3" /></div><div>Rating: 4.3/<strong>5</strong> (6 votes cast)</div><br /><a target="_blank" href="http://www.gdstarrating.com/"><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx/powered.png" border="0" width="80" height="15" /></a><br />]]></description>
			<content:encoded><![CDATA[<p>This is a very simple question with a not so simple answer&#8230;</p>
<p>Forget for a second about .NET.<br />
If someone asks you how much is 1.000025 &#8211; 0.000025 your answer will probably be &#8220;1&#8243; and correct. An easy question, right ?</p>
<p>Now, let&#8217;s go back to .NET and check if our calculations are correct.</p>
<p><span id="more-360"></span></p>
<pre class="c-sharp" name="code">        static void Main(string[] args)
        {
            double d1 = 1.000025;
            double d2 = 0.000025;
            Console.WriteLine((d1 - d2) == 1);
        }</pre>
<p>If you think the result will be &#8220;true&#8221;&#8230; well, it&#8217;s not.</p>
<p>Let&#8217;s see another example :</p>
<pre class="c-sharp" name="code">        static void Main(string[] args)
        {
            double d1 = 1.000025;
            double d2 = 0.000025;

            // Will result 1
            Console.WriteLine(d1 - d2);

            // Will result 0.99999999999999989
            Console.WriteLine((d1 - d2).ToString ("R"));
        }</pre>
<p>For those who might not know, the meaning of ToString(&#8220;R&#8221;) is <a title="MSDN - Standard Numeric Format Strings" href="http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx" target="_blank">Round-trip</a>.</p>
<blockquote><p>This format is supported only for the Single and Double types. The round-trip specifier guarantees that a numeric value converted to a string will be parsed back into the same numeric value. When a numeric value is formatted using this specifier, it is first tested using the general format, with 15 spaces of precision for a Double and 7 spaces of precision for a Single. If the value is successfully parsed back to the same numeric value, it is formatted using the general format specifier. However, if the value is not successfully parsed back to the same numeric value, then the value is formatted using 17 digits of precision for a Double and 9 digits of precision for a Single.</p></blockquote>
<p>So, what happened here ? Is it a bug ?<br />
The answer is &#8211; NO, IT&#8217;S NOT A BUG ( and it&#8217;s not happening only on my computer ).</p>
<p>The thing is, that the above is about how .NET rounding a floating point number.<br />
I won&#8217;t explain here the math ( there is already a great <a title="What Every Computer Scientist Should Know About Floating-Point Arithmetic" href="http://docs.sun.com/source/806-3568/ncg_goldberg.html" target="_blank">article</a> explaining it ), and i won&#8217;t say don&#8217;t use floating point types.</p>
<p>You just need to know that when you are using it &#8211; be carefull, especialy in equations and formatting, otherwise your calculations will be wrong and your result not accurate.</p>
<br /><div><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx.php?value=4.3" /></div><div>Rating: 4.3/<strong>5</strong> (6 votes cast)</div><br /><a target="_blank" href="http://www.gdstarrating.com/"><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx/powered.png" border="0" width="80" height="15" /></a><br />
<p class="FacebookLikeButton"><fb:like href="http%3A%2F%2Fkossovsky.net%2Findex.php%2F2009%2F07%2Fcsharp-the-double-trouble%2F" layout="standard" show_faces="false" width="450" action="like" colorscheme="light"></fb:like></p>
]]></content:encoded>
			<wfw:commentRss>http://kossovsky.net/index.php/2009/07/csharp-the-double-trouble/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C# string.Empty vs &#8220;&#8221;</title>
		<link>http://kossovsky.net/index.php/2009/06/string-empty-versus-empty-quotes/</link>
		<comments>http://kossovsky.net/index.php/2009/06/string-empty-versus-empty-quotes/#comments</comments>
		<pubDate>Sun, 28 Jun 2009 22:08:33 +0000</pubDate>
		<dc:creator>Xander</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Interview Questions]]></category>
		<category><![CDATA[Optimization]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[c# string]]></category>
		<category><![CDATA[c#.net]]></category>
		<category><![CDATA[c-sharp]]></category>
		<category><![CDATA[compilation error]]></category>
		<category><![CDATA[console switch]]></category>
		<category><![CDATA[empty]]></category>
		<category><![CDATA[empty quotes]]></category>
		<category><![CDATA[empty string]]></category>
		<category><![CDATA[net c#]]></category>
		<category><![CDATA[puzzles]]></category>
		<category><![CDATA[quotes]]></category>
		<category><![CDATA[string]]></category>
		<category><![CDATA[string value]]></category>
		<category><![CDATA[string.cs]]></category>
		<category><![CDATA[string.Empty]]></category>
		<category><![CDATA[test string]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[two ways]]></category>
		<category><![CDATA[variable value]]></category>
		<category><![CDATA[visual c#]]></category>
		<category><![CDATA[writeline]]></category>

		<guid isPermaLink="false">http://kossovsky.net/?p=263</guid>
		<description><![CDATA[As we know we have two ways to set a variable value to an empty string. We can just set it to empty quotes &#8220;&#8221; or set it to string.Empty, but what is the difference and is there one at all ? If we will look at the .NET String.cs we will see the next [...]<br /><div><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx.php?value=4.2" /></div><div>Rating: 4.2/<strong>5</strong> (9 votes cast)</div><br /><a target="_blank" href="http://www.gdstarrating.com/"><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx/powered.png" border="0" width="80" height="15" /></a><br />]]></description>
			<content:encoded><![CDATA[<p>As we know we have two ways to set a variable value to an empty string.<br />
We can just set it to empty quotes &#8220;&#8221; or set it to string.Empty, but what is the difference and is there one at all ?</p>
<p><span id="more-263"></span><br />
If we will look at the .NET String.cs we will see the next line:</p>
<pre class="c-sharp" name="code">
        public static readonly String Empty = "";
</pre>
<p>The above line can tell us that string.Empty is not a constant as we would like to believe but a readonly field.</p>
<p>The question is why, why static readonly and not a constant. The answer is in the same String.cs comments of the Empty field.</p>
<pre class="c-sharp" name="code">
        // The Empty constant holds the empty string value.
        //We need to call the String constructor so that the compiler doesn't mark this as a literal.
        //Marking this as a literal would mean that it doesn't show up as a field which we can access
        //from native.
</pre>
<p>Now, let&#8217;s make some test.</p>
<pre class="c-sharp" name="code">
            string EmptyValue = string.Empty;

            if (EmptyValue == "")
                // Output : Is Empty : 1
                Console.WriteLine("Is Empty : 1");

            if (EmptyValue.Length == 0)
                // Output : Is Empty : 2
                Console.WriteLine("Is Empty : 2");

            switch (EmptyValue)
            {
                case "":
                    {
                        // Output : Is Empty : 3
                        Console.WriteLine("Is Empty : 3");
                        break;
                    }
                 case string.Empty:
                    {
                        // Compilation error : A constant value is expected
                        Console.WriteLine("A constant value is expected");
                        break;
                    }
            }

            if (EmptyValue.Equals (""))
                // Output : Is Empty : 4
                Console.WriteLine("Is Empty : 4");

            if (string.IsNullOrEmpty(EmptyValue))
                // Output : Is Empty : 5
                Console.WriteLine("Is Empty : 5");
</pre>
<p>As we can see string.Empty and &#8220;&#8221; have the same behavior except the &#8220;switch&#8221; because string.Empty is not a constant.</p>
<p>And of course the most important question &#8211; is there a performance differences between the two ? The answer is &#8211; Yes, but minor.<br />
Lets see some results :</p>
<pre class="c-sharp" name="code">
           // Execution Time : 2144ms
            for (int n = 0; n < 100000000; n++)
            {
                if (TestValue == string.Empty)
                    t = 1; //"For test purposes only..."
            }

            // Execution Time : 2211ms
            for (int i = 0; i < 100000000; i++)
            {
                if (TestValue == "")
                    t = 2; //"For test purposes only..."
            }
</pre>
<p>We can see from the results that the comparison with string.Empty is a little bit faster but it's negligible.</p>
<p>One of the advantages of using "" is that it's in some cases it can be optimized by the compiler before the JIT. For example if you will run the same iteration while comparing ""==null the whole block will be removed by the optimizer.</p>
<p>So, use what ever you like, you'd probably won't see the difference.</p>
<br /><div><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx.php?value=4.2" /></div><div>Rating: 4.2/<strong>5</strong> (9 votes cast)</div><br /><a target="_blank" href="http://www.gdstarrating.com/"><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx/powered.png" border="0" width="80" height="15" /></a><br />
<p class="FacebookLikeButton"><fb:like href="http%3A%2F%2Fkossovsky.net%2Findex.php%2F2009%2F06%2Fstring-empty-versus-empty-quotes%2F" layout="standard" show_faces="false" width="450" action="like" colorscheme="light"></fb:like></p>
]]></content:encoded>
			<wfw:commentRss>http://kossovsky.net/index.php/2009/06/string-empty-versus-empty-quotes/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>CSS Sprite Generator</title>
		<link>http://kossovsky.net/index.php/2009/06/css-sprite-generator/</link>
		<comments>http://kossovsky.net/index.php/2009/06/css-sprite-generator/#comments</comments>
		<pubDate>Sun, 14 Jun 2009 21:29:32 +0000</pubDate>
		<dc:creator>Xander</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Graphics]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[Optimization]]></category>
		<category><![CDATA[c-sharp]]></category>
		<category><![CDATA[composite image]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[CSS Sprite]]></category>
		<category><![CDATA[css sprites]]></category>
		<category><![CDATA[multiple images]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[positioning]]></category>
		<category><![CDATA[Puzzle]]></category>
		<category><![CDATA[Speed]]></category>
		<category><![CDATA[sprite]]></category>

		<guid isPermaLink="false">http://kossovsky.net/?p=3</guid>
		<description><![CDATA[CSS sprites group multiple images into one composite image and display them using CSS background positioning. Coming soon… Rating: 0.0/5 (0 votes cast)<br /><div><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx.php?value=0.0" /></div><div>Rating: 0.0/<strong>5</strong> (0 votes cast)</div><br /><a target="_blank" href="http://www.gdstarrating.com/"><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx/powered.png" border="0" width="80" height="15" /></a><br />]]></description>
			<content:encoded><![CDATA[<p>CSS sprites group multiple images into one composite image and display them using CSS background positioning.</p>
<p>Coming soon…</p>
<br /><div><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx.php?value=0.0" /></div><div>Rating: 0.0/<strong>5</strong> (0 votes cast)</div><br /><a target="_blank" href="http://www.gdstarrating.com/"><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx/powered.png" border="0" width="80" height="15" /></a><br />
<p class="FacebookLikeButton"><fb:like href="http%3A%2F%2Fkossovsky.net%2Findex.php%2F2009%2F06%2Fcss-sprite-generator%2F" layout="standard" show_faces="false" width="450" action="like" colorscheme="light"></fb:like></p>
]]></content:encoded>
			<wfw:commentRss>http://kossovsky.net/index.php/2009/06/css-sprite-generator/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

