|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
StringBuilder size question?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 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 > This is great news! Thanks for your reply.
>-----Original Message----- a great option when >Assigning all the space to a StringBuilder in advance is >you know how much space you'll need, but falling back to StringBuilder will grow as string >concatenation is not your only other option. A >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 >> > > >. >
Determine if "Hide extensions for known file types" is active
What scope is best for defining Enum type? VB6 to VB.Net Conversion Selecting specific columns from a dataview? How to overload method of third party component? creating an array as property of a class Limiting the directories somebody can browse to..... Passing parameters to Data Adapter Creating a custom log Hierarchal recordset |
|||||||||||||||||||||||