<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Optimizing C# String Performance</title><link>http://blogs.msdn.com/charlie/archive/2006/10/11/Optimizing-C_2300_-String-Performance.aspx</link><description>Strings in C# are highly optimized but also potentially very wasteful. They give programmers a safe, fast way to handle character data. However, there are a few tricks you need to know about strings and memory if you want to write efficient code. Without</description><dc:language>en</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: Optimizing C# String Performance</title><link>http://blogs.msdn.com/charlie/archive/2006/10/11/Optimizing-C_2300_-String-Performance.aspx#816747</link><pubDate>Wed, 11 Oct 2006 13:55:52 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:816747</guid><dc:creator>Nitin</dc:creator><description>&lt;p&gt;Interesting new information!&lt;/p&gt;
&lt;p&gt;Thanks.&lt;/p&gt;
</description></item><item><title>New and Notable Links : Sep 27 - Oct 10 2006</title><link>http://blogs.msdn.com/charlie/archive/2006/10/11/Optimizing-C_2300_-String-Performance.aspx#816756</link><pubDate>Wed, 11 Oct 2006 13:59:44 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:816756</guid><dc:creator>Dan's Archive</dc:creator><description /></item><item><title>Charlie Calvert's Community Blog : Optimizing C# String Performance</title><link>http://blogs.msdn.com/charlie/archive/2006/10/11/Optimizing-C_2300_-String-Performance.aspx#819547</link><pubDate>Thu, 12 Oct 2006 16:16:25 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:819547</guid><dc:creator>DotNetKicks.com</dc:creator><description>&lt;p&gt;You've been kicked (a good thing) - Trackback from DotNetKicks.com&lt;/p&gt;
</description></item><item><title>Charlie Calvert's Technical Blog Index</title><link>http://blogs.msdn.com/charlie/archive/2006/10/11/Optimizing-C_2300_-String-Performance.aspx#1059121</link><pubDate>Sat, 11 Nov 2006 20:05:24 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1059121</guid><dc:creator>Charlie Calvert's Community Blog</dc:creator><description>&lt;p&gt;This is index to the various technical posts I have created for this blog. As I add to each section,&lt;/p&gt;
</description></item><item><title>Important Lesson</title><link>http://blogs.msdn.com/charlie/archive/2006/10/11/Optimizing-C_2300_-String-Performance.aspx#1061190</link><pubDate>Sun, 12 Nov 2006 05:53:23 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1061190</guid><dc:creator>Helpware</dc:creator><description>&lt;p&gt;One of the most important lessons to learn in C#. Actually there are parallels here with Delphi aren't there? C# String managment =similar= Delphi Huge String managment. C# StringBuilder managment =simpilar= Delphi strings[256]. ? Rob&lt;/p&gt;
</description></item><item><title>re: Optimizing C# String Performance</title><link>http://blogs.msdn.com/charlie/archive/2006/10/11/Optimizing-C_2300_-String-Performance.aspx#1070017</link><pubDate>Mon, 13 Nov 2006 21:12:50 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1070017</guid><dc:creator>Charlie Calvert</dc:creator><description>&lt;p&gt;Rob,&lt;/p&gt;
&lt;p&gt;Though we can't always be sure who made what decisions when, nevertheless Ander's fingerprints are present in both Delphi and C#, and so it is no surprise that we keep running across interesting parallels between the two languages. Delphi made strings so easy to use that it was difficult to improve on the system, but I think C# keeps that simplicity while adding a few twists that significantly improve usability. In particular, I find it nice that C# strings are a real class, while in Delphi they are just a glorified primitive type. But this is not really a put down of Delphi. Everything in Delphi for Win32 is elegant and highly performant.&lt;/p&gt;
</description></item><item><title>re: Optimizing C# String Performance</title><link>http://blogs.msdn.com/charlie/archive/2006/10/11/Optimizing-C_2300_-String-Performance.aspx#1078131</link><pubDate>Wed, 15 Nov 2006 01:18:43 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1078131</guid><dc:creator>Helpware</dc:creator><description>&lt;p&gt;Agreed. Thanks for the reply.&lt;/p&gt;</description></item><item><title>re: Optimizing C# String Performance</title><link>http://blogs.msdn.com/charlie/archive/2006/10/11/Optimizing-C_2300_-String-Performance.aspx#1684075</link><pubDate>Thu, 15 Feb 2007 18:22:55 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1684075</guid><dc:creator>Anon</dc:creator><description>&lt;p&gt;Sorry your explanations are not really that good. All you've pointed out is that the compiler is really good and optimizing the code you've written. Well you did mention interning strings ....&lt;/p&gt;
&lt;p&gt;You don't touch on the fact that strings are immutable nor what the performance gotcha's are, various scenarios, and the canonical way to deal with them. &lt;/p&gt;
&lt;p&gt;So where's the optimisation?&lt;/p&gt;
&lt;p&gt;Just because the compiler is smarter than you doesn't mean when you're writing code you should be lazy - it's better to do it the correct way. &lt;/p&gt;
&lt;p&gt;e.g. The compiler, when seeing hapless coder write:&lt;/p&gt;
&lt;p&gt;string result = &amp;quot;value=&amp;quot; + x + &amp;quot;, and other= &amp;quot; + y + &amp;quot;.&amp;quot; &lt;/p&gt;
&lt;p&gt;is optimisied by the compiler to&lt;/p&gt;
&lt;p&gt;string result = new StringBuilder().Append(&amp;quot;value=&amp;quot;).Append(x).Append(&amp;quot;, and other=&amp;quot;).Append(y).Append(&amp;quot;.&amp;quot;).ToString();&lt;/p&gt;
&lt;p&gt;So it tries to save you from &amp;nbsp;5 separate allocations in that line, but it can't optimise it completely especially if there are more concatenations from one line to the next.&lt;/p&gt;
</description></item><item><title>re: Optimizing C# String Performance</title><link>http://blogs.msdn.com/charlie/archive/2006/10/11/Optimizing-C_2300_-String-Performance.aspx#4943888</link><pubDate>Sun, 16 Sep 2007 21:09:52 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:4943888</guid><dc:creator>David Cumps</dc:creator><description>&lt;p&gt;Some additional information about strings and memory usage which you might find interesting: &lt;a rel="nofollow" target="_new" href="http://blog.cumps.be/string-concatenation-vs-memory-allocation/"&gt;http://blog.cumps.be/string-concatenation-vs-memory-allocation/&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;It seems strings are quite a complex topic after all ;)&lt;/p&gt;
</description></item><item><title>re: Optimizing C# String Performance</title><link>http://blogs.msdn.com/charlie/archive/2006/10/11/Optimizing-C_2300_-String-Performance.aspx#6283617</link><pubDate>Fri, 16 Nov 2007 04:58:35 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6283617</guid><dc:creator>Charlie Calvert</dc:creator><description>&lt;p&gt;Anon,&lt;/p&gt;
&lt;p&gt;Thanks for showing an example of using the StringBuilder class. Though I did mention both it and the immutability of strings, my post probably would have been stronger had I included an example of how to use StringBuilder.&lt;/p&gt;
&lt;p&gt;David,&lt;/p&gt;
&lt;p&gt;Thank you for your reference to your excellent post on String Allocation vs Memory Allocation. For people who want to dig further beneath the surface this is a great reference.&lt;/p&gt;
&lt;p&gt;I also should have pointed out that performance problems in applications rarely stem from code that performs string concatenation or allocation. As a rule, it is best to look elsewhere if you find that your application is performing poorly. This is not to say that we cannot optimize string performance, only that it is usually not an important bottleneck.&lt;/p&gt;
&lt;p&gt;- Charlie&lt;/p&gt;
</description></item><item><title>re: Optimizing C# String Performance</title><link>http://blogs.msdn.com/charlie/archive/2006/10/11/Optimizing-C_2300_-String-Performance.aspx#7748256</link><pubDate>Sun, 17 Feb 2008 11:09:45 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7748256</guid><dc:creator>Washier</dc:creator><description>&lt;p&gt;Charlie,&lt;/p&gt;
&lt;p&gt;I thought your explanations were that good. I think Anon failed to see that your article intentionally left readers to explore the subject a bit further and apply it in their own little world - we all write very different software.&lt;/p&gt;
&lt;p&gt;The &amp;quot;hapless&amp;quot; coder writing string = string + string type code is most likely working on software not requiring super-performance. This poor coder is also working on a tight schedule, maybe stressed out. This coder doesn't have time, and should not be spending time on optimizing code that in most cases turns to be a very small part of the whole.&lt;/p&gt;
&lt;p&gt;In my case performance is very important, on the back-end that is. And I will go and check my code again after I reading Charlie's work.&lt;/p&gt;
&lt;p&gt;This is what makes the language great. It relies on the user's intuition and talent rather than knowledge(which comes with time anyway).&lt;/p&gt;
</description></item><item><title>re: Optimizing C# String Performance</title><link>http://blogs.msdn.com/charlie/archive/2006/10/11/Optimizing-C_2300_-String-Performance.aspx#8351512</link><pubDate>Wed, 02 Apr 2008 17:30:55 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8351512</guid><dc:creator>Ricky Shrestha</dc:creator><description>&lt;p&gt;Thanks for the post. This was just the information I needed and with the help of this post (by changing string to stringbuilder) I got more than 120 folds performance increase. All I was doing was going round a loop concatenating some string. Now I use StringBuilder. Thanks for the post.&lt;/p&gt;
</description></item></channel></rss>