You are currently offline, waiting for your internet to reconnect

How To Improve String Concatenation Performance

This article was previously published under Q170964
SUMMARY
When concatenating large strings on the order of 50kb or larger (for example, building an HTML table from a database), the length of time to complete can become quite long as the string gets larger. This article demonstrates an alternative to normal concatenation that can improve performance for large strings by 20 times or more.
MORE INFORMATION
When performing repeated concatenations of the type:
   For I = 1 To N     Dest = Dest & Source   Next N				
the length of time increases proportionally to N-squared. Therefore, 1000 iterations will take about 100 times longer than 100 iterations. This is because Visual Basic does not just add the Source characters to the end of the Dest string; it also performs the following operations:

  1. Allocates temporary memory large enough to hold the result.
  2. Copies Dest to the start of the temporary area.
  3. Copies Source to the end of the temporary area.
  4. De-allocates the old copy of Dest.
  5. Allocates memory for Dest large enough to hold the result.
  6. Copies the temporary data to Dest.
Steps 2 and 6 are very expensive and basically result in the entire concatenated result being copied twice with additional overhead to allocate and de-allocate memory.

This article details a method using the Mid$ statement and pre-allocating memory in larger chunks to eliminate all but step 3 above for most of the concatenation phase.

WARNING: ANY USE BY YOU OF THE CODE PROVIDED IN THIS ARTICLE IS AT YOUR OWN RISK. Microsoft provides this code "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.

Step-by-Step Example

  1. Type the following code into a module:
          Option Explicit      ' For 16-bit products, uncomment the next three lines by removing the      ' single quotes and add a single quote to comment out the following      ' three lines.      '  Const ConcatStr = "ABC"      '  Const ccIncrement = 15000      '  Declare Function GetTickCount Lib "USER" () As Long         Const ConcatStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"         Const ccIncrement = 50000         Private Declare Function GetTickCount Lib "KERNEL32" () As Long      Dim ccOffset As Long      Sub StdConcat(ByVal LoopCount As Long)      Dim BigStr As String, I As Long, StartTick As Long        StartTick = GetTickCount()        For I = 1 To LoopCount          BigStr = BigStr & ConcatStr        Next I        Debug.Print LoopCount; "concatenations took";        Debug.Print GetTickCount() - StartTick; "ticks"      End Sub      Sub Test_Concat()        Debug.Print "Using standard concatenation"        StdConcat 1000        StdConcat 2000        StdConcat 3000        StdConcat 4000        StdConcat 5000        Debug.Print        Debug.Print "Using pre-allocated storage and pseudo-concatenation"        MidConcat 1000        MidConcat 2000        MidConcat 3000        MidConcat 4000        MidConcat 5000      End Sub      Sub Concat(Dest As String, Source As String)      Dim L As Long        L = Len(Source)        If (ccOffset + L) >= Len(Dest) Then          If L > ccIncrement Then            Dest = Dest & Space$(L)          Else            Dest = Dest & Space$(ccIncrement)          End If        End If        Mid$(Dest, ccOffset + 1, L) = Source        ccOffset = ccOffset + L      End Sub      Sub MidConcat(ByVal LoopCount As Long)      Dim BigStr As String, I As Long, StartTick As Long        StartTick = GetTickCount()        ccOffset = 0        For I = 1 To LoopCount          Concat BigStr, ConcatStr        Next I        BigStr = Left$(BigStr, ccOffset)        Debug.Print LoopCount; "pseudo-concatenations took";        Debug.Print GetTickCount() - StartTick; "ticks"      End Sub						
  2. In the Debug/Immediate Window, type Test_Concat, and hit the Enter key.

    The results will look similar to:
          Using standard concatenation       1000 concatenations took 2348 ticks       2000 concatenations took 8954 ticks       3000 concatenations took 20271 ticks       4000 concatenations took 35103 ticks       5000 concatenations took 54453 ticks      Using pre-allocated storage and pseudo-concatenation       1000 pseudo-concatenations took 82 ticks       2000 pseudo-concatenations took 124 ticks       3000 pseudo-concatenations took 165 ticks       4000 pseudo-concatenations took 247 ticks       5000 pseudo-concatenations took 289 ticks

Additional Information

  1. The code may take a couple of minutes to run.
  2. GetTickCount returns the number of milliseconds since Windows was started. Therefore, the output is in milliseconds.
  3. Performance improvement ranges from almost 30 times for the 1000-iteration case to almost 200 times for the 5000-iteration case. These times may vary depending on:

    • The product used.
    • Your system configuration..
    • The size of ccIncrement (larger size favors MidConcat).
    • The number of iterations used (more iterations favors MidConcat).
    • The size of the resultant string (larger size favors MidConcat).
REFERENCES
For additional information, click the following article number to view the article in the Microsoft Knowledge Base:
306821 How To Improve String Concatenation Performance in Visual Basic .NET
Properties

Article ID: 170964 - Last Review: 10/11/2006 18:40:47 - Revision: 3.7

  • Microsoft Visual Basic 5.0 Control Creation Edition
  • Microsoft Visual Basic 5.0 Learning Edition
  • Microsoft Visual Basic 5.0 Professional Edition
  • Microsoft Visual Basic 5.0 Enterprise Edition
  • Microsoft Visual Basic 4.0 Standard Edition
  • Microsoft Visual Basic 4.0 Professional Edition
  • Microsoft Visual Basic 4.0 Professional Edition
  • Microsoft Visual Basic 4.0 16-bit Enterprise Edition
  • Microsoft Visual Basic 4.0 32-Bit Enterprise Edition
  • Microsoft Visual Basic 3.0 Professional Edition
  • Microsoft Visual Basic 3.0 Professional Edition
  • Microsoft Access 1.0 Standard Edition
  • Microsoft Access 1.1 Standard Edition
  • Microsoft Access 2.0 Standard Edition
  • Microsoft Access 95 Standard Edition
  • Microsoft Access 97 Standard Edition
  • Microsoft Excel 95 Standard Edition
  • Microsoft Excel 97 Standard Edition
  • Microsoft Word 97 Standard Edition
  • kbhowto kbprogramming KB170964
Feedback