<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://msmvps.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 String Concatenation in VBA</title><link>http://msmvps.com/blogs/nateoliver/archive/2009/05/23/optimizing-string-concatenation-in-vba.aspx</link><description>Good day, This probably should have been my first blog entry, as my code structures often look at using buffers when dealing with dynamic Arrays and Strings. Today we will have a brief look at Strings. When building a String you want to avoid a large</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP2 (Build: 40407.4157)</generator><item><title>re: Optimizing String Concatenation in VBA</title><link>http://msmvps.com/blogs/nateoliver/archive/2009/05/23/optimizing-string-concatenation-in-vba.aspx#1693654</link><pubDate>Tue, 02 Jun 2009 16:12:20 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1693654</guid><dc:creator>Nate Oliver</dc:creator><description>&lt;p&gt;That might be true if I was suggesting that my example be used in all cases, and it was just an example, and I think it could be more realistic than you appear to think. &lt;/p&gt;
&lt;p&gt;But you did note the following?&lt;/p&gt;
&lt;p&gt;&amp;quot;So, often times it makes a lot more sense, performance-wise, to work with Arrays, whether it&amp;#39;s String &amp;amp; Byte Arrays, which directly coerce, or Array-&amp;gt;String via Join() - with a buffered, 1D Array.&amp;quot;&lt;/p&gt;
&lt;p&gt;Your example, while trivial code, requires a different approach, so change faster() to this:&lt;/p&gt;
&lt;p&gt;Sub Faster(strVal() As String)&lt;/p&gt;
&lt;p&gt;Dim tmpStr As String&lt;/p&gt;
&lt;p&gt;Let tmpStr = Join(strVal, vbNullString)&lt;/p&gt;
&lt;p&gt;End Sub&lt;/p&gt;
&lt;p&gt;This essentially does the same thing as slower(), and is on the order of ~100% faster, on my machine.&lt;/p&gt;
&lt;p&gt;I&amp;#39;m not quite following this comment:&lt;/p&gt;
&lt;p&gt;&amp;quot;It&amp;#39;s fastest to allocate the largest possible buffer up front (all free system memory?), but you waste system resources that way.&amp;quot;&lt;/p&gt;
&lt;p&gt;I get the part about largest possible buffer, as otherwise there&amp;#39;d be overhead to moniter where you&amp;#39;re at in the buffer and adjust it, otherwise. But I&amp;#39;m not sure about all free system memory?&lt;/p&gt;
&lt;p&gt;I doubt you&amp;#39;re going to chew up that much memory building a buffered String in VBA?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1693654" width="1" height="1"&gt;</description></item><item><title>re: Optimizing String Concatenation in VBA</title><link>http://msmvps.com/blogs/nateoliver/archive/2009/05/23/optimizing-string-concatenation-in-vba.aspx#1693629</link><pubDate>Tue, 02 Jun 2009 07:02:57 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1693629</guid><dc:creator>fzz</dc:creator><description>&lt;p&gt;You do realize that your example isn&amp;#39;t realistic? If not, replace your statements&lt;/p&gt;
&lt;p&gt;strVal(0) = 0: &amp;nbsp;strVal(1) = 1: &amp;nbsp;strVal(2) = 2&lt;/p&gt;
&lt;p&gt;strVal(3) = 3: &amp;nbsp;strVal(4) = 4: &amp;nbsp;strVal(5) = 5&lt;/p&gt;
&lt;p&gt;with&lt;/p&gt;
&lt;p&gt;strVal(0) = &amp;quot;a&amp;quot;&lt;/p&gt;
&lt;p&gt;strVal(1) = &amp;quot;bb&amp;quot;&lt;/p&gt;
&lt;p&gt;strVal(2) = &amp;quot;ccc&amp;quot;&lt;/p&gt;
&lt;p&gt;strVal(3) = &amp;quot;dddd&amp;quot;&lt;/p&gt;
&lt;p&gt;strVal(4) = &amp;quot;eeeee&amp;quot;&lt;/p&gt;
&lt;p&gt;strVal(5) = &amp;quot;ffffff&amp;quot;&lt;/p&gt;
&lt;p&gt;convert your faster and slower routines to functions, and check the respective function results. Your slower routine at least produces the correct result, &amp;quot;abbcccddddeeeeeffffff&amp;quot;. Your faster routine produces the incorrect result &amp;quot;abcdef&amp;quot;. It&amp;#39;s easy to produce wrong answers quickly. Most people don&amp;#39;t even need code to do that.&lt;/p&gt;
&lt;p&gt;It takes a fair amount of nonobvious overhead code to handle preallocated buffers that don&amp;#39;t eat too much memory. The technique is discussed in Kernighan &amp;amp; Pike, &amp;#39;The Practice of Programming&amp;#39;, chapter 4. In brief, you start off with a moderately large buffer, and when you need to grow the buffer, you double it. You need to keep track of the buffer size and how much of the buffer has been used.&lt;/p&gt;
&lt;p&gt;It&amp;#39;s fastest to allocate the largest possible buffer up front (all free system memory?), but you waste system resources that way.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1693629" width="1" height="1"&gt;</description></item></channel></rss>