Francesco's blog

 Saturday, December 22, 2007

When you migrate code from VB6 - regardless of whether you are using an automatic conversion tool - chances are that string-intensive code will actually run slower under VB.NET, if it uses a lot of string concatenation operations. For example, this code takes 2.8 seconds when it runs in VB6 and 27 seconds after its conversion to VB.NET on my 3GHz system:

Dim s As String = ""
For i As Integer = 1 To 100000
    s = s + "*"
Next

The solution is of course trivial: just replace the string variable with a StringBuilder object. Alas, this fix requires that you completely revise your source code, because you need to replace all + and & operators with the Append method, not to mention cases where the StringBuilder is used as an argument to string functions such as Trim or Len.

Is there a way to speed up the previous code with a minimal impact on the code itself? The answer is yes and the solution is actually very simple: you just need to create a VB.NET class that is based on the System.Text.StringBuilder object, that redifines the + and & operators, and that supports the implicit conversion to and from the System.String type. Authoring such a StringBuilder6 class is a matter of minutes:

' a wrapper for the StringBuilder object, with support for + and & operators

Public Class StringBuilder6

    Private buffer As New System.Text.StringBuilder

    ' return the inner string

    Public Overrides Function ToString() As String
       
Return buffer.ToString()
    End Function

    Public Shared Operator +(ByVal op1 As StringBuilder6, ByVal op2 As String) As StringBuilder6
        op1.buffer.Append(op2)
       
Return op1
    End Operator

    Public Shared Operator &(ByVal op1 As StringBuilder6, ByVal op2 As String) As StringBuilder6
        op1.buffer.Append(op2)
        Return op1
    End Operator

    ' convert to string

    Public Shared Widening Operator CType(ByVal op As StringBuilder6) As String
       
Return op.ToString()
    End Operator

    ' convert from string

    Public Shared Widening Operator CType(ByVal str As String) As StringBuilder6
        Dim op As New StringBuilder6()
        op.buffer.Append(str)
        Return op
    End Operator

End Class

Once the StringBuilder6 class is in place, you just need to replace the type of the String variable:

Dim s As StringBuilder6 = ""

After this edit, the loop runs in 0.008 seconds, that is about 2000 times faster!!! Not bad, for such a simple fix :-)

Regardless of whether you are migrating code from VB6 or you've written VB.NET or C# code from scratch, the StringBuilder class gives you a quick and simple way to check whether your string concatenations can be optimized by resorting to a StringBuilder.

 

6/12/2008 5:42:52 PM (GMT Daylight Time, UTC+01:00)
Hi Francesco
I got your huge book Programming Microsoft Visual Basic.Net 2003, but I was so surprised that it has no mention to the reports or sub-reports in VB... My first question is how come you left it out and the second which books or material do you recommend for this ?
10/8/2008 2:29:50 PM (GMT Daylight Time, UTC+01:00)
This may seem like a mundane topic, but I was wondering if you think this could be implemented with extension methods in .NET 3.0?
Chris Gomez
5/18/2009 5:20:44 PM (GMT Daylight Time, UTC+01:00)
Sorry. I used to think that the brain was the most wonderful organ in my body. Then I realized who was telling me this.
I am from Sri and learning to speak English, tell me right I wrote the following sentence: "Website design fl search engine optimization services.We put the pieces of seo together for you so you feel confident in your ability."

With respect ;), Verena.
9/16/2009 8:29:51 AM (GMT Daylight Time, UTC+01:00)
http://www.maplestory4mesos.com/Gold.html is a Specialized MapleStory Mesos store,we sell Maple Story Mesos,you can buy Safe Maple Story Mesos,Cheap Maple Story Mesos from us."
9/25/2009 5:24:56 PM (GMT Daylight Time, UTC+01:00)
Once i moved from Classic VB to VB.Net. the first thing i changed after migration is started using the String Builder for large concatenations.

Nice post with good analysis
12/2/2009 1:05:33 PM (GMT Standard Time, UTC+00:00)
i found it very difficult to work with .net as it is working with full of classes. now I can easily code it
12/15/2009 2:43:33 AM (GMT Standard Time, UTC+00:00)
Welcome to our web
2/2/2010 5:21:25 AM (GMT Standard Time, UTC+00:00)
[url=http://www.discountukghd.com/]ghd[/url] [url=http://www.discountukghd.com/]ghd straighteners[/url] are being sold at low prices. you will get [url=http://www.discountukghd.com/]cheap ghd[/url], try to keep a piece of [url=http://www.discountukghd.com/ghd-black-c-8.html]ghd black[/url].
Name
E-mail
Home page

Comment (HTML not allowed)  

Enter the code shown (prevents robots):

 
Get RSS/Atom Feed
RSS 2.0 | Atom 1.0
Search in the blog
Archive
<March 2010>
SunMonTueWedThuFriSat
28123456
78910111213
14151617181920
21222324252627
28293031123
45678910
Categories

Powered by: newtelligence dasBlog 1.8.5223.1