Home All Groups Group Topic Archive Search About

using StringBuilder class for concatenation?

Author
30 Mar 2005 4:35 PM
Ron
Hello,

I have to concatenate some large strings which end up in a
text file.  I am just checking if the StringBuilder class
can improve what I am currently doing - and how to
implement this.  Here is what I am currently doing:

Do while...true
   ...
   strData += str1 & ColDelimeter
   ...
   strData += RowDelimeter
   oWrite.WriteLine(strData)
   strData = ""
Loop

Each row is about 10k in size, average and hundreds of
thousands of rows.  Can the StringBuilder Class improve
what I am doing?  May I request a sample how to use it?

Thanks,
Ron

Author
30 Mar 2005 5:01 PM
Robin Tucker
Yes it most definately will.  It will speed it up by an order of magnitude
(in my experience).

Usage is simple:


Dim sb as new StringBuilder

sb = "whatever"
sb = sb + " more whatever"

oWrite.Writeline (sb.ToString)

However, I see you are writing a line at a time (oWrite.WriteLine).  If you
have hundreds of thousands of lines, perhaps you should add carriage
return/line feeds with your string build and batch the writes, say, 1000
lines at a time?


Show quoteHide quote
"Ron" <anonym***@discussions.microsoft.com> wrote in message
news:0c5f01c53546$6fbc6e50$a601280a@phx.gbl...
> Hello,
>
> I have to concatenate some large strings which end up in a
> text file.  I am just checking if the StringBuilder class
> can improve what I am currently doing - and how to
> implement this.  Here is what I am currently doing:
>
> Do while...true
>   ...
>   strData += str1 & ColDelimeter
>   ...
>   strData += RowDelimeter
>   oWrite.WriteLine(strData)
>   strData = ""
> Loop
>
> Each row is about 10k in size, average and hundreds of
> thousands of rows.  Can the StringBuilder Class improve
> what I am doing?  May I request a sample how to use it?
>
> Thanks,
> Ron
>
>
Author
30 Mar 2005 5:01 PM
Jay B. Harlow [MVP - Outlook]
Ron,
| Can the StringBuilder Class improve
| what I am doing?
Yes a StringBuilder can improve what you are doing. However I have to ask
why bother with string concatenation & StringBuilder at all. I would simply
write to the file.

| May I request a sample how to use it? (StringBuilder)

Dim strData As StringBuilder
| Do while...true
    ' Each row is about 10k in size
    strData = New StringBuilder(10 * 1024)
|   ...
|   strData += str1 & ColDelimeter
    strData.Append(str1)
    strData.Append(ColDelimeter)
|   ...
|   strData += RowDelimeter
    strData.Append(RowDelimeter)
|   oWrite.WriteLine(strData.ToString())
| Loop


| May I request a sample how to use it? (write to the file)

| Do while...true
|   ...
|   strData += str1 & ColDelimeter
    oWrite.Write(str1)
    oWrite.Write(ColDelimeter)
|   ...
|   strData += RowDelimeter
    oWrite.Write(RowDelimeter)
|   oWrite.WriteLine()
| Loop

Depending on how you opened the file it will be buffered, using the correct
FileStream constructor allows you to increase the size of the buffer.

Be mindful of RowDelimeter, if its CR & LF, WriteLine will write that for
you & you may be doubling your rows.

Hope this helps
Jay

Show quoteHide quote
"Ron" <anonym***@discussions.microsoft.com> wrote in message
news:0c5f01c53546$6fbc6e50$a601280a@phx.gbl...
| Hello,
|
| I have to concatenate some large strings which end up in a
| text file.  I am just checking if the StringBuilder class
| can improve what I am currently doing - and how to
| implement this.  Here is what I am currently doing:
|
| Do while...true
|   ...
|   strData += str1 & ColDelimeter
|   ...
|   strData += RowDelimeter
|   oWrite.WriteLine(strData)
|   strData = ""
| Loop
|
| Each row is about 10k in size, average and hundreds of
| thousands of rows.  Can the StringBuilder Class improve
| what I am doing?  May I request a sample how to use it?
|
| Thanks,
| Ron
|
|
Author
30 Mar 2005 5:30 PM
Ron
Thanks all for your replies.  I think StringBuilder will
help.  My deal is that I have to massage each piece of
data that I read from the external source - it contains
all kinds of stuff like formfeed chars, linefeed chars,
etc.  I do not use cr & lf chars for rowdelimeter, more
like ||##.  The textfile(s) eventually gets sucked up by a
DTS package.  So I did not want to take any chances using
delimeters that may be chars that are already in the data
I am retrieving, so I made up my own custom delimeters. 
Then I run a DTS package (com) that I translated to VB.Net.

BTW, anyone know when VS2005 is due out?  I can't wait to
get my hands on the bulkDataTransfer class.  I have been
having some issues using the DTS package in VB6 and
VB.Net. 

Anyway, thanks again for the help.

Ron


Show quoteHide quote
>-----Original Message-----
>Hello,
>
>I have to concatenate some large strings which end up in
a
>text file.  I am just checking if the StringBuilder class
>can improve what I am currently doing - and how to
>implement this.  Here is what I am currently doing:
>
>Do while...true
>   ...
>   strData += str1 & ColDelimeter
>   ...
>   strData += RowDelimeter
>   oWrite.WriteLine(strData)
>   strData = ""
>Loop
>
>Each row is about 10k in size, average and hundreds of
>thousands of rows.  Can the StringBuilder Class improve
>what I am doing?  May I request a sample how to use it?
>
>Thanks,
>Ron
>
>
>.
>
Author
30 Mar 2005 10:44 PM
Dick Grier
Hi,

>>
My deal is that I have to massage each piece of
data that I read from the external source
<<

If you have to "massage" then a StringBuilder may not help.  StingBuilder is
designed to improve appending (strings are immutable).  However, to
manipulate data that is part of a StringBuilder, you have to convert it to a
String.  Thus (in general) you loose the performance gain that StringBuilder
provides for the append process.  In fact, I expect that your performance
will be much poorer if you use SB.

Dick

--
Richard Grier  (Microsoft Visual Basic MVP)

See www.hardandsoftware.net for contact information.

Author of Visual Basic Programmer's Guide to Serial Communications, 4th
Edition ISBN 1-890422-28-2  (391 pages) published July 2004.  See
www.mabry.com/vbpgser4 to order.