<?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>Bitmatic &#187; performance</title>
	<atom:link href="http://bitmatic.com/tag/performance/feed" rel="self" type="application/rss+xml" />
	<link>http://bitmatic.com</link>
	<description>Defragmenting a .NET programmers brain</description>
	<lastBuildDate>Tue, 13 Jul 2010 13:15:58 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Fixing a slow scrolling DataGridView</title>
		<link>http://bitmatic.com/c/fixing-a-slow-scrolling-datagridview</link>
		<comments>http://bitmatic.com/c/fixing-a-slow-scrolling-datagridview#comments</comments>
		<pubDate>Sun, 25 Oct 2009 12:14:43 +0000</pubDate>
		<dc:creator>Jakob</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[controls]]></category>
		<category><![CDATA[DataGridView]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[Scroll]]></category>
		<category><![CDATA[VS2008]]></category>

		<guid isPermaLink="false">http://bitmatic.com/?p=476</guid>
		<description><![CDATA[Whenever your C#/.NET DataGridView reaches a certain size, it tends to get really slow to scroll. Depending on the speed of your computer this may be more or less noticeable. In an application i did for a client this became a real problem due to a combination of lots of DataGridView cells and fairly slow [...]]]></description>
			<content:encoded><![CDATA[<p>Whenever your C#/.NET DataGridView reaches a certain size, it tends to get really slow to scroll. Depending on the speed of your computer this may be more or less noticeable. In an application i did for a client this became a real problem due to a combination of lots of DataGridView cells and fairly slow computers.<br />
Luckily the solution turned out to be simple&#8230;</p>
<h3>Turn on double buffering</h3>
<p>Turning on double buffering seems to solve the problem. Normally double buffering would only help reduce flickering, since painting is being done to an off-screen buffer, but for the DataGridView it also significantly reduces the amount of functions being called internally in the DataGridView &#8211; thus reducing processor load and increasing speed. (statistics gathered with the <a href="http://www.eqatec.com/tools/tracer">Eqatec Tracer</a>)</p>
<h3>My DataGridView doesn&#8217;t have a DoubleBuffered property !?!?</h3>
<p>For some reason Microsoft has decided to hide the DoubleBuffered property from DataGridView. Luckily you can set it anyway with reflection.<br />
<!-- code formatted by http://manoli.net/csharpformat/ --></p>
<pre class="csharpcode">
<span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">class</span> ExtensionMethods
{
    <span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">void</span> DoubleBuffered(<span class="kwrd">this</span> DataGridView dgv, <span class="kwrd">bool</span> setting)
    {
        Type dgvType = dgv.GetType();
        PropertyInfo pi = dgvType.GetProperty(<span class="str">"DoubleBuffered"</span>,
            BindingFlags.Instance | BindingFlags.NonPublic);
        pi.SetValue(dgv, setting, <span class="kwrd">null</span>);
    }
}</pre>
<p>Just drop the above class into your project somewhere, or add the function to your existing extension methods.<br />
The extension method allows you to set the DoubleBuffered property on your DataGridView in the following manner:<br />
<!-- code formatted by http://manoli.net/csharpformat/ --></p>
<pre class="csharpcode">
dataGridView1.DoubleBuffered(<span class="kwrd">true</span>);</pre>
<p>You now have a smooth scrolling DataGridView <img src='http://bitmatic.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://bitmatic.com/c/fixing-a-slow-scrolling-datagridview/feed</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
		<item>
		<title>Understanding thread priorities in C#</title>
		<link>http://bitmatic.com/c/understanding-thread-priorities-in-c</link>
		<comments>http://bitmatic.com/c/understanding-thread-priorities-in-c#comments</comments>
		<pubDate>Mon, 21 Sep 2009 20:14:57 +0000</pubDate>
		<dc:creator>Jakob</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[Threads]]></category>

		<guid isPermaLink="false">http://bitmatic.com/?p=434</guid>
		<description><![CDATA[Thread priorities in the .NET framework are more complex than many developers think. Setting Thread priority is more than just adjusting the ThreadPriority of a Thread. You also have to take into account the priority of the process to which the threads belong.
The ProcessPriorityClass
Setting ThreadPriority to one of the values possible with C# will only [...]]]></description>
			<content:encoded><![CDATA[<p>Thread priorities in the .NET framework are more complex than many developers think. Setting Thread priority is more than just adjusting the ThreadPriority of a Thread. You also have to take into account the priority of the process to which the threads belong.</p>
<h3>The ProcessPriorityClass</h3>
<p>Setting ThreadPriority to one of the values possible with C# will only shift the priority of the thread up and down within a class of priorities shared by the whole process. This means that even setting ThreadPriority to ThreadPriority.Highest will not actually give the thread a high priority, compared to the system processes. It will only give the thread a high priority compared to other threads belonging to comparable processes.<br />
I&#8221;l try to make it a bit clearer with this little css/ascii illustration <img src='http://bitmatic.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<div style="width:100%;">
<div style="position:relative;left:15px;background:#a6ddea;width:465px;font-size:75%;">Full system priority range: 1-31</div>
<div style="position:relative;left:30px;background:#a6ddea;width:60px;font-size:75%;">Idle</div>
<div style="position:relative;left:60px;background:#a6ddea;width:60px;font-size:75%;">BelowNormal</div>
<div style="position:relative;left:90px;background:#a6ddea;width:60px;font-size:75%;">Normal</div>
<div style="position:relative;left:120px;background:#a6ddea;width:60px;font-size:75%;">AboveNormal</div>
<div style="position:relative;left:165px;background:#a6ddea;width:60px;font-size:75%;">High</div>
<div style="position:relative;left:330px;background:#a6ddea;width:60px;font-size:75%;">RealTime</div>
</div>
<p>In C# processes start with normal priority, so setting ThreadPriority.Highest under normal conditions wont make the thread higher priority than ThreadPriority.Lowest in a process running with a high priority class.</p>
<h3>Letting Windows know that you are really important.</h3>
<p>Take a look at this code:<br />
<!-- code formatted by http://manoli.net/csharpformat/ --></p>
<pre class="csharpcode">
<span class="kwrd">class</span> Program
{
  <span class="kwrd">static</span> <span class="kwrd">void</span> Main(<span class="kwrd">string</span>[] args)
  {
    Thread t = <span class="kwrd">new</span> Thread(threadFunc);
    Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.RealTime;
    t.Priority = ThreadPriority.Highest;
    t.Start();
    Console.ReadLine();
    t.Abort();
  }

  <span class="kwrd">static</span> <span class="kwrd">void</span> threadFunc()
  {
    <span class="kwrd">while</span> (<span class="kwrd">true</span>) {}
  }
}</pre>
<p>This code will make a thread/process that has the highest possible priority you can set with C#. Luckily i have a dual-core processor &#8211; otherwise that code would have frozen my computer. Now it only uses up one of my cores frantically checking if true is still true&#8230;<br />
The magic here lies in assigning ProcessPriorityClass.RealTime to the current process. Note that this is done in a process-wide scope, and all the threads in the process will have their priorities adjusted to fit in the new priority range. You can not have threads with extremely high and extremely low priorities within the same process.</p>
<h3>It is really cool &#8211; but don&#8217;t abuse it.</h3>
<p>Do not run off and adjust the priorities of all your programs, to get the processing time they rightly deserve. Fixing problems by raising priorities are very often a symptom of some greater underlying problem in your code, and the correct way of fixing things are most often to remove the bottlenecks in your program, and leave the priorities alone. This holds especially true for ProcessPriorityClass.RealTime, since this will potentially block execution of critical system stuff like garbage collection or keyboard and mouse input.<br />
I would recommend against using anything higher than ProcessPriorityClass.High</p>
]]></content:encoded>
			<wfw:commentRss>http://bitmatic.com/c/understanding-thread-priorities-in-c/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>String &amp; StringBuilder performance in the Compact Framework</title>
		<link>http://bitmatic.com/compact-framework/string-stringbuilder-performance-in-the-compact-framework</link>
		<comments>http://bitmatic.com/compact-framework/string-stringbuilder-performance-in-the-compact-framework#comments</comments>
		<pubDate>Tue, 28 Jul 2009 17:01:48 +0000</pubDate>
		<dc:creator>Jakob</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Compact Framework]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[string]]></category>

		<guid isPermaLink="false">http://bitmatic.com/?p=395</guid>
		<description><![CDATA[Here&#8217;s a few general guidelines when working with strings:

Don&#8217;t concatenate strings &#8211; use a StringBuilder instead
Whenever possible initialize the StringBuilder with enough capacity to avoid re-allocations
Excessive concatenation of strings will be several orders of magnitude slower than using a StringBuilder
String.Format() &#038; StringBuilder.AppendFormat() are significantly slower than just using the +operator or Append() &#8211; but may [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a few general guidelines when working with strings:</p>
<ul>
<li>Don&#8217;t concatenate strings &#8211; use a StringBuilder instead</li>
<li>Whenever possible initialize the StringBuilder with enough capacity to avoid re-allocations</li>
<li>Excessive concatenation of strings will be several orders of magnitude slower than using a StringBuilder</li>
<li>String.Format() &#038; StringBuilder.AppendFormat() are significantly slower than just using the +operator or Append() &#8211; but may be more readable</li>
<li>When performance optimizing an application &#8211; string usage is a good place to start</li>
</ul>
<h3>Don&#8217;t concatenate strings</h3>
<p>The <a href="http://msdn.microsoft.com/en-us/library/system.text.stringbuilder.aspx">StringBuilder</a> is an often overlooked class, that can be used to solve a lot of performance problems in your applications.<br />
To give an example of how ineffective the basic string can be, take a look at the following code that builds 2 large strings by concatenating 1000 times to the same string using String and StringBuilder:<br />
<!-- code formatted by http://manoli.net/csharpformat/ --></p>
<pre class="csharpcode">
<span class="kwrd">string</span> s = <span class="kwrd">string</span>.Empty;
<span class="kwrd">for</span> (<span class="kwrd">int</span> j = 0; j &lt; 1000; j++)
{
  s += <span class="str">"Test of string performance."</span>;
}

StringBuilder sb = <span class="kwrd">new</span> StringBuilder();
<span class="kwrd">for</span> (<span class="kwrd">int</span> j = 0; j &lt; 1000; j++)
{
  sb.Append(<span class="str">"Test of string performance."</span>);
}
</pre>
<p>Performance is measured on my HTC S710 moblie phone. The performance difference is very noticeable.</p>
<ul>
<li>The String implementation takes about 800ms</li>
<li>The StringBuilder implementation takes about 12ms</li>
</ul>
<p>StringBuilder is almost 70 times faster than String in this particular example and initializing the StringBuilder with enough capacity up front will shave another 2ms off. I have done tons of optimizations on Compact Framework applications over the years, and the first thing i usually do is replace String with StringBuilder in code similar to the above example.</p>
<h3>Immutable strings &#8211; blessing or curse?</h3>
<p>Strings in the .NET framework are immutable &#8211; meaning that once they are created they can not be changed. It is a design decision from the .NET framework designers, that allows for a lot of optimizations, but also cost performance under certain circumstances.<br />
This is the main reason the String concatenation from before is so slow. Every time the strings are concatenated, a new String is created, and the old one is left for the garbage collector. At least a thousand strings of increasing size are created, copied and garbage collected for no good reason. It is amazingly ineffective.</p>
<h3>StringBuilder &#8211; definitely a blessing!</h3>
<p>The StringBuilder is the thing to use, when handling and manipulating large strings. It holds the string in an internal array that it grows as needed. This enables a very fast append that only rarely allocates anything new. In my test the StringBuilder initially had a capacity of 64 bytes, and grew to double size whenever needed, but implementations may vary.<br />
This behaviour means that you can optimize performance even further by initializing the StringBuilder to a size that is slightly larger than what you expect the final string to be &#8211; thereby removing the need to ever re-allocate.</p>
<h3>A word or two about String.Format()</h3>
<p>I always assumed that using String.Format() to splice strings with variables was the fastest way of doing it. Tests show, however, that it is not&#8230;<br />
I have made test of 4 different ways of accomplishing the same thing:<br />
<!-- code formatted by http://manoli.net/csharpformat/ --></p>
<pre class="csharpcode">
<span class="rem">//using string</span>
<span class="kwrd">string</span> x = <span class="str">"Test of string performance."</span> + 1 + 2 + <span class="str">"hello"</span>;
<span class="kwrd">string</span> x = <span class="kwrd">string</span>.Format(<span class="str">"Test of string performance.{0}{1}{2}"</span>, 1, 2, <span class="str">"hello"</span>);

<span class="rem">//using StringBuilder</span>
sb.Append(<span class="str">"Test of string performance."</span>).Append(1).Append(2).Append(<span class="str">"hello"</span>);
sb.AppendFormat(<span class="str">"Test of string performance.{0}{1}{2}"</span>, 1, 2, <span class="str">"hello"</span>);</pre>
<p>The results are similar for both the String and StringBuilder. Using the +operator or chained appends is <b>roughly twice as fast</b> as using the Format() method. This came as a bit of a surprise to me.</p>
]]></content:encoded>
			<wfw:commentRss>http://bitmatic.com/compact-framework/string-stringbuilder-performance-in-the-compact-framework/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
