<?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>Olaf's Thoughts About Development &#187; garbage collector</title>
	<atom:link href="http://www.monien.net/blog/index.php" rel="self" type="application/rss+xml" />
	<link>http://www.monien.net/blog</link>
	<description>Delphi Programming, .NET Philosophy, Web development and more ...</description>
	<lastBuildDate>Mon, 14 Mar 2011 15:59:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.2</generator>
		<item>
		<title>Delphi 2009: TStringBuilder (Recap and Benchmark)</title>
		<link>http://www.monien.net/blog/index.php/2008/10/delphi-2009-tstringbuilder/</link>
		<comments>http://www.monien.net/blog/index.php/2008/10/delphi-2009-tstringbuilder/#comments</comments>
		<pubDate>Tue, 14 Oct 2008 12:32:05 +0000</pubDate>
		<dc:creator>Olaf Monien</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Delphi]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[AnsiString]]></category>
		<category><![CDATA[garbage collector]]></category>
		<category><![CDATA[TStringBuilder]]></category>
		<category><![CDATA[Unicode]]></category>

		<guid isPermaLink="false">http://www.monien.net/blog/index.php/2008/10/delphi-2009-tstringbuilder/</guid>
		<description><![CDATA[There are a lot of new things to learn with Delphi’s new Unicode string type(s). For example look at Lars’ blog. Quite some interesting posts over the last days. Apparently, the new Delphi 2009 TStringBuilder class has not got much attention yet, so I’ll give a short recap here. Under .NET there is a StringBuilder [...]]]></description>
			<content:encoded><![CDATA[<p>There are a lot of new things to learn with Delphi’s new Unicode string type(s). For example look at <a href="http://compaspascal.blogspot.com" target="_blank">Lars’ blog</a>. Quite some interesting posts over the last days.</p>
<p>Apparently, the new Delphi 2009 TStringBuilder class has not got much attention yet, so I’ll give a short recap here.</p>
<p><span id="more-214"></span><br />
Under .NET there is a StringBuilder class since the very beginning. This is because in .NET Strings are <em>immutable,</em> which means that once the content of a string instance is set, it is fixed. It cannot be changed anymore. “Quasi modifying” operations require new instances to be created and the old ones to be discarded:</p>
<p>s := s + ‘a’  means “create a new string instance and initialize it with the old content of s plus an ‘a’, then throw the old instance s pointed to into the garbage collector”</p>
<p>Lot of pressure on the memory management system that is.</p>
<p>Thus StringBuilder was invented. The core idea is to prepare some buffer and let all operations work on that buffer, so that you don’t need to recreate instances all the time. The buffer is still managed, and all are happy.</p>
<p>The question is now: “why do we need a TStringBuilder class in Delphi/Win32, and if it is a “real” class (i.e. not just a highly optimized type in System.pas), could it be really fast?”</p>
<p>Well, it appears to be fast, look at this code (test environment: Core 2 Duo, 2GHz):</p>
<pre class="brush: delphi">

// approx 2.2 sec ~45M operations/sec
LStart := GetTickCount;
for i := 0 to 100000000 do begin
SB.Append(&#039; &#039;);
end;
LStop := GetTickCount;
Writeln(&#039;StringBuilder: &#039;, LStop - LStart);

// approx 3.8 sec ~ 26M operations/sec
s:=&#039;&#039;;
LStart := GetTickCount;
for i := 0 to 100000000 do begin
s := s + &#039; &#039;;
end;
LStop := GetTickCount;
Writeln(&#039;String: &#039;, LStop - LStart);

// approx 3.5 sec ~ 28M operations/sec
a:=&#039;&#039;;
LStart := GetTickCount;
for i := 0 to 100000000 do begin
a := a + &#039; &#039;;
end;
LStop := GetTickCount;
Writeln(&#039;AnsiString: &#039;, LStop - LStart);
</pre>
<div id="scid:fb3a1972-4489-4e52-abe7-25a00bb07fdf:10d82691-db74-445e-a107-81b76ed1a4a7" class="wlWriterEditableSmartContent" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<p>Download source: <a href="http://www.monien.net/blog/wp-content/uploads/lw/Delphi2009TStringBuilder_CC5E/StringBenchmark.zip" target="_blank">StringBenchmark.zip</a></p>
</div>
<p>So obviously TStringBuilder is useful for certain operations, as its significantly faster than String. As expected AnsiString is faster than String, but its still slower than TStringBuilder.</p>
<p>So this leaves to investigate when TStringBuilder has advantages, besides in this fairly artificial case. To be continued …</p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center shr-bookmarks-bg-knowledge">
<ul class="socials">
		<li class="shr-twitter">
			<a href="http://www.shareaholic.com/api/share/?title=Delphi+2009%3A+TStringBuilder+%28Recap+and+Benchmark%29&amp;link=http://www.monien.net/blog/index.php/2008/10/delphi-2009-tstringbuilder/&amp;notes=There%20are%20a%20lot%20of%20new%20things%20to%20learn%20with%20Delphi%E2%80%99s%20new%20Unicode%20string%20type%28s%29.%20For%20example%20look%20at%20Lars%E2%80%99%20blog.%20Quite%20some%20interesting%20posts%20over%20the%20last%20days.%0D%0A%0D%0AApparently%2C%20the%20new%20Delphi%202009%20TStringBuilder%20class%20has%20not%20got%20much%20attention%20yet%2C%20so%20I%E2%80%99ll%20give%20a%20short%20recap%20here.%0D%0A%0D%0A%0D%0AUnder%20&amp;short_link=&amp;shortener=tinyurl&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=%24%7Btitle%7D+-+%24%7Bshort_link%7D&amp;service=7&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
		<li class="shr-comfeed">
			<a href="http://www.monien.net/blog/index.php/2008/10/delphi-2009-tstringbuilder/feed" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
		<li class="shr-delicious">
			<a href="http://www.shareaholic.com/api/share/?title=Delphi+2009%3A+TStringBuilder+%28Recap+and+Benchmark%29&amp;link=http://www.monien.net/blog/index.php/2008/10/delphi-2009-tstringbuilder/&amp;notes=There%20are%20a%20lot%20of%20new%20things%20to%20learn%20with%20Delphi%E2%80%99s%20new%20Unicode%20string%20type%28s%29.%20For%20example%20look%20at%20Lars%E2%80%99%20blog.%20Quite%20some%20interesting%20posts%20over%20the%20last%20days.%0D%0A%0D%0AApparently%2C%20the%20new%20Delphi%202009%20TStringBuilder%20class%20has%20not%20got%20much%20attention%20yet%2C%20so%20I%E2%80%99ll%20give%20a%20short%20recap%20here.%0D%0A%0D%0A%0D%0AUnder%20&amp;short_link=&amp;shortener=tinyurl&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=2&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-digg">
			<a href="http://www.shareaholic.com/api/share/?title=Delphi+2009%3A+TStringBuilder+%28Recap+and+Benchmark%29&amp;link=http://www.monien.net/blog/index.php/2008/10/delphi-2009-tstringbuilder/&amp;notes=There%20are%20a%20lot%20of%20new%20things%20to%20learn%20with%20Delphi%E2%80%99s%20new%20Unicode%20string%20type%28s%29.%20For%20example%20look%20at%20Lars%E2%80%99%20blog.%20Quite%20some%20interesting%20posts%20over%20the%20last%20days.%0D%0A%0D%0AApparently%2C%20the%20new%20Delphi%202009%20TStringBuilder%20class%20has%20not%20got%20much%20attention%20yet%2C%20so%20I%E2%80%99ll%20give%20a%20short%20recap%20here.%0D%0A%0D%0A%0D%0AUnder%20&amp;short_link=&amp;shortener=tinyurl&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=3&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-googlebuzz">
			<a href="http://www.shareaholic.com/api/share/?title=Delphi+2009%3A+TStringBuilder+%28Recap+and+Benchmark%29&amp;link=http://www.monien.net/blog/index.php/2008/10/delphi-2009-tstringbuilder/&amp;notes=There%20are%20a%20lot%20of%20new%20things%20to%20learn%20with%20Delphi%E2%80%99s%20new%20Unicode%20string%20type%28s%29.%20For%20example%20look%20at%20Lars%E2%80%99%20blog.%20Quite%20some%20interesting%20posts%20over%20the%20last%20days.%0D%0A%0D%0AApparently%2C%20the%20new%20Delphi%202009%20TStringBuilder%20class%20has%20not%20got%20much%20attention%20yet%2C%20so%20I%E2%80%99ll%20give%20a%20short%20recap%20here.%0D%0A%0D%0A%0D%0AUnder%20&amp;short_link=&amp;shortener=tinyurl&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=257&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Post on Google Buzz">Post on Google Buzz</a>
		</li>
		<li class="shr-reddit">
			<a href="http://www.shareaholic.com/api/share/?title=Delphi+2009%3A+TStringBuilder+%28Recap+and+Benchmark%29&amp;link=http://www.monien.net/blog/index.php/2008/10/delphi-2009-tstringbuilder/&amp;notes=There%20are%20a%20lot%20of%20new%20things%20to%20learn%20with%20Delphi%E2%80%99s%20new%20Unicode%20string%20type%28s%29.%20For%20example%20look%20at%20Lars%E2%80%99%20blog.%20Quite%20some%20interesting%20posts%20over%20the%20last%20days.%0D%0A%0D%0AApparently%2C%20the%20new%20Delphi%202009%20TStringBuilder%20class%20has%20not%20got%20much%20attention%20yet%2C%20so%20I%E2%80%99ll%20give%20a%20short%20recap%20here.%0D%0A%0D%0A%0D%0AUnder%20&amp;short_link=&amp;shortener=tinyurl&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=40&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-technorati">
			<a href="http://www.shareaholic.com/api/share/?title=Delphi+2009%3A+TStringBuilder+%28Recap+and+Benchmark%29&amp;link=http://www.monien.net/blog/index.php/2008/10/delphi-2009-tstringbuilder/&amp;notes=There%20are%20a%20lot%20of%20new%20things%20to%20learn%20with%20Delphi%E2%80%99s%20new%20Unicode%20string%20type%28s%29.%20For%20example%20look%20at%20Lars%E2%80%99%20blog.%20Quite%20some%20interesting%20posts%20over%20the%20last%20days.%0D%0A%0D%0AApparently%2C%20the%20new%20Delphi%202009%20TStringBuilder%20class%20has%20not%20got%20much%20attention%20yet%2C%20so%20I%E2%80%99ll%20give%20a%20short%20recap%20here.%0D%0A%0D%0A%0D%0AUnder%20&amp;short_link=&amp;shortener=tinyurl&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=10&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on Technorati">Share this on Technorati</a>
		</li>
</ul><div style="clear: both;"></div><div class="shr-getshr" style="visibility:hidden;font-size:10px !important"><a target="_blank" href="http://www.shareaholic.com/?src=pub">Get Shareaholic</a></div><div style="clear: both;"></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.monien.net/blog/index.php/2008/10/delphi-2009-tstringbuilder/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>

