HOWTO: Improve String Concatenation Performance

ID: Q170964


The information in this article applies to:
  • Microsoft Visual Basic Control Creation, Learning, Professional, and Enterprise Editions for Windows, version 5.0
  • Microsoft Visual Basic Standard, Professional, and Enterprise Editions, 16-bit and 32-bit, for Windows, version 4.0
  • Microsoft Visual Basic Standard and Professional Editions for Windows, version 3.0
  • Microsoft Access versions 1.0, 1.1, 2.0, 7.0, 97
  • Microsoft Excel for Windows 95, version 7.0
  • Microsoft Excel 97 for Windows
  • Microsoft Word 97 for Windows


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. Thus, 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. Thus, 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).




Additional query words:

Keywords : kbprg kbVBp400 kbVBp500 kbhowto VB4WIN VBKBVBA vbwin
Version : WINDOWS:1.0,1.1,2.0,3.0,4.0,5.0,7.0,97
Platform : WINDOWS
Issue type : kbhowto


Last Reviewed: October 20, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.