Previous | Index | Next 

[HOWTO] Speed up string concatenation

.NET code might run slower than the original VB6 code when it contains many string concatenations, especially if inside a loop. If you are an experienced .NET developer, you might be aware that you can often optimize such code by replacing the string variable with a StringBuilder object. However, this replacement isn’t quite painless, because you also need to replace all & and + operators with calls to the StringBuilder’s Append method.

VB Migration Partner offers a neat solution to this problem by means of the StringBuilder6 class. Suppose that you have the following code:

        Dim s As String = ""
        Dim i As Integer
        For i = 1 To 10000
            s = s + Chr(i Mod 256)
        Next

On a 3GHz system, this loop takes about 2.8 seconds in VB6 and over 17 seconds in VB.NET. However, if you replace just the declaration of the variable as follows:

        Dim s As StringBuilder6 = ""

then the VB.NET version takes just 0.03 seconds, thus becoming 90+ times faster than the original VB6 code and abot 550 times faster than the VB.NET code obtained by a plain migration. (Of course you get the same speed improvement if you are converting to C#.)

You don’t need to manually change the type of the string variable after the migration process. Once you have determined which string variables would benefit from adopting the new StringBuilder6 class, you can use an opportune SetType pragma in the original VB6 code:

        '## s.SetType StringBuilder6
        Dim s As String

Interestingly, VB Migration Partner automatically marks string concatenation operations inside a For, Do, or While loop by means of a migration warning, as in the following VB.NET code:

        Dim s As String = ""
        Dim i As Integer
        For i = 1 To 10000
            ' UPGRADE_INFO (#0571): String concatenation inside a loop.
            ' Consider declaring the 's' variable as a StringBuilder6 object.
            s = s + Chr(i Mod 256)
        Next

Changing the type of a variable can have side-effects elsewhere in the project. For some tips on how you can work around these issues, read this article.

 

IMPORTANT NOTE: The StringBuilder6 objects differ from standard strings for another, important detail: the concatenation operator (& or +) always modifies its left operand and sets it equal to the result of the concatenation. This somewhat unexpected behavior may introduce subtle bugs in your  code. Consider the following code snippet:

        Dim s As StringBuilder6 = ""
        Dim t As String
		
        ' CASE 1: assigning to same variable
        s = s & "ABC"    ' s is now equal to “ABC” (expected behavior)
        ' CASE 2: assigning to different variable
        t = s & "123"    ' s is now equal to “ABC123” (potential issue)

As you see, everything works well if you are appending characters to the same StringBuilder6 object (case 1): the & operator changes the variable to “123” and then the variable is assigned to itself, but the result is OK.

When assigning to a different variable (case 2), however, the StringBuilder6 variable is assigned the concatenated string only because it is the left operand of the & operator, even if it isn’t the target of the assignment. As a result, after the concatenation the StringBuilder6 variable contains a value that you might not expect.

Always keep this detail in mind when replacing a standard string with a StringBuilder6 object.

 

Previous | Index | Next