|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
using StringBuilder class for concatenation?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 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 > > Ron,
| Can the StringBuilder Class improve Yes a StringBuilder can improve what you are doing. However I have to ask | what I am doing? 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 sizestrData = New StringBuilder(10 * 1024) | ... strData.Append(str1)| strData += str1 & ColDelimeter strData.Append(ColDelimeter) | ... strData.Append(RowDelimeter)| strData += RowDelimeter | oWrite.WriteLine(strData.ToString()) oWrite.Write(str1)| Loop | May I request a sample how to use it? (write to the file) | Do while...true | ... | strData += str1 & ColDelimeter oWrite.Write(ColDelimeter) | ... oWrite.Write(RowDelimeter)| strData += RowDelimeter | oWrite.WriteLine() Depending on how you opened the file it will be buffered, using the correct | Loop 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 | | 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 > > >. > Hi,
>> My deal is that I have to massage each piece ofdata 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.
Advice needed
How can I determine WHICH exception I got in my CATCH? Using The NativeWindow Class To Draw A GDI Type Circle On Top Of A DataGrid Possibly In The Override String.Split versus Strings.Split CSV-Datei einlesen How to restrict the multiple opening of the Same window... a checkbox in a datagrid Process.Start("WinWord.exe") problem add dsn programmatorically tooltip on combobox selection |
|||||||||||||||||||||||