Home All Groups Group Topic Archive Search About

StringBuilder size question?

Author
15 Apr 2005 9:41 PM
Ron
Hello,

I have to read the contents of a textfile and would like
to read it row by row.  Each row contains about 180
fields.  Most of the data is varchar, but some text fields
(memo fields).  So row size varies from 10k to 50k.  If I
declare my stringbuilder var like this:

Dim strData As New StringBuilder(50 * 1024)

am I wasting space?  Will this defeat the benefits of
using stringbuilder when a row contains only 5k of data as
I loop through the file?  My alternative would be this:

Dim str1 As string, i as integer, arrStr as Array
....'arrStr contains contents of the row
For i = 0 to arrStr.Length - 1
  str1 += arrStr.GetValue(i).ToString & ", "
Next

Thanks,
Ron

Author
15 Apr 2005 10:13 PM
Russell Jones
Assigning all the space to a StringBuilder in advance is a great option when
you know how much space you'll need, but falling back to string
concatenation is not your only other option. A StringBuilder will grow as
needed to accomodate whatever content you Append to it, so that should be
your fallback position--just use a StringBuilder (without pre-setting the
size) and Append. That will still be much faster than plain string
concatenation.

Dim strData As New StringBuilder()
Dim i as integer, arrStr as Array
'arrStr contains contents of the row
For i = 0 to arrStr.Length - 1
   strData.Append arrStr.GetValue(i).ToString & ", "
Next


Show quoteHide quote
"Ron" <anonym***@discussions.microsoft.com> wrote in message
news:09c201c54203$ea38e720$a501280a@phx.gbl...
> Hello,
>
> I have to read the contents of a textfile and would like
> to read it row by row.  Each row contains about 180
> fields.  Most of the data is varchar, but some text fields
> (memo fields).  So row size varies from 10k to 50k.  If I
> declare my stringbuilder var like this:
>
> Dim strData As New StringBuilder(50 * 1024)
>
> am I wasting space?  Will this defeat the benefits of
> using stringbuilder when a row contains only 5k of data as
> I loop through the file?  My alternative would be this:
>
> Dim str1 As string, i as integer, arrStr as Array
> ...'arrStr contains contents of the row
> For i = 0 to arrStr.Length - 1
>  str1 += arrStr.GetValue(i).ToString & ", "
> Next
>
> Thanks,
> Ron
>
Author
15 Apr 2005 10:39 PM
Ron
This is great news!  Thanks for your reply.


>-----Original Message-----
>Assigning all the space to a StringBuilder in advance is
a great option when
>you know how much space you'll need, but falling back to
string
>concatenation is not your only other option. A
StringBuilder will grow as
>needed to accomodate whatever content you Append to it,
so that should be
>your fallback position--just use a StringBuilder (without
pre-setting the
Show quoteHide quote
>size) and Append. That will still be much faster than
plain string
>concatenation.
>
>Dim strData As New StringBuilder()
>Dim i as integer, arrStr as Array
>'arrStr contains contents of the row
>For i = 0 to arrStr.Length - 1
>   strData.Append arrStr.GetValue(i).ToString & ", "
>Next
>
>
>"Ron" <anonym***@discussions.microsoft.com> wrote in
message
>news:09c201c54203$ea38e720$a501280a@phx.gbl...
>> Hello,
>>
>> I have to read the contents of a textfile and would like
>> to read it row by row.  Each row contains about 180
>> fields.  Most of the data is varchar, but some text
fields
>> (memo fields).  So row size varies from 10k to 50k.  If
I
>> declare my stringbuilder var like this:
>>
>> Dim strData As New StringBuilder(50 * 1024)
>>
>> am I wasting space?  Will this defeat the benefits of
>> using stringbuilder when a row contains only 5k of data
as
>> I loop through the file?  My alternative would be this:
>>
>> Dim str1 As string, i as integer, arrStr as Array
>> ...'arrStr contains contents of the row
>> For i = 0 to arrStr.Length - 1
>>  str1 += arrStr.GetValue(i).ToString & ", "
>> Next
>>
>> Thanks,
>> Ron
>>
>
>
>.
>