473,396 Members | 2,158 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Most efficiant use of stringbuilder

Hello everyone, I have a question for you. If I need to print 20,000
lines to a file which option is the most efficient?

(these are generated inside a datareader loop)

#1 Concatenate the data into a string variable, then write file at the
end of the loop (99.9% sure this is not it)
#2 Append the data into stringbuilder variable, then write file at the
end of the loop
#3 Append the data into stringbuilder variable, writing to the file
ever 500 or so records
#4 Don't use any variable, simply write directly to the file in every
iteration (99.9% sure this is not it either)

Thanks, I really appreciate your help. Have a great day!
Ryan
Nov 21 '05 #1
2 3703
The fastest way--by far--is #4: Write directly to the file, using a buffered
StreamWriter.

"Ryan McLean" <Ry*********@NAU.EDU> wrote in message
news:6e**************************@posting.google.c om...
Hello everyone, I have a question for you. If I need to print 20,000
lines to a file which option is the most efficient?

(these are generated inside a datareader loop)

#1 Concatenate the data into a string variable, then write file at the
end of the loop (99.9% sure this is not it)
#2 Append the data into stringbuilder variable, then write file at the
end of the loop
#3 Append the data into stringbuilder variable, writing to the file
ever 500 or so records
#4 Don't use any variable, simply write directly to the file in every
iteration (99.9% sure this is not it either)

Thanks, I really appreciate your help. Have a great day!
Ryan

Nov 21 '05 #2
Ryan,
Hello everyone, I have a question for you. If I need to print 20,000
lines to a file which option is the most efficient? #4 Don't use any variable, simply write directly to the file in every
iteration (99.9% sure this is not it either)
If I were writing to a file, I would write to the file. So your #4 is the
correct choice.

As Russell suggests use a StreamWriter, depending on how you create your
StreamWriter it is buffered!

However I would write the routine with the assumption of a TextWriter, this
way you can use either a StreamWriter & write to a file or a StringWriter &
write to a StringBuilder.

Something like:
Private Shared Sub OutputReader(ByVal reader As Data.IDataReader, ByVal
writer As TextWriter)
Do While reader.Read()
writer.Write(reader.GetInt32(0))
writer.Write(","c)
writer.Write(reader.GetString(1))
writer.WriteLine()
Loop
End Sub

Public Shared Sub Main()
Dim reader As SqlClient.SqlDataReader
Dim writer As New IO.StreamWriter("myfile.csv")
OutputReader(reader, writer)
writer.Close()

Dim writer As New IO.StringWriter
OutputReader(reader, writer)
writer.close()
Dim s As string = writer.ToString()

End Sub

Hope this helps
Jay

"Ryan McLean" <Ry*********@NAU.EDU> wrote in message
news:6e**************************@posting.google.c om... Hello everyone, I have a question for you. If I need to print 20,000
lines to a file which option is the most efficient?

(these are generated inside a datareader loop)

#1 Concatenate the data into a string variable, then write file at the
end of the loop (99.9% sure this is not it)
#2 Append the data into stringbuilder variable, then write file at the
end of the loop
#3 Append the data into stringbuilder variable, writing to the file
ever 500 or so records
#4 Don't use any variable, simply write directly to the file in every
iteration (99.9% sure this is not it either)

Thanks, I really appreciate your help. Have a great day!
Ryan

Nov 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Alvin Bruney | last post by:
Is string builder intelligent enough to handle concats without behaving like string? Consider myStringBuilder.Append("one" + "two") what does the '+' do here? Because this syntax is also...
0
by: Mo | last post by:
I am having problem with marshaling struct in C#. //the original C++ struct typedef struct _tagHHP_DECODE_MSG { DWORD dwStructSize; // Size of decode structure. TCHAR ...
11
by: deko | last post by:
I need to loop through a string and remove all characters except numbers or letters. I am getting an ArgumentOutOfRangeException: "Index was out of range. Must be non-negative and less than the...
2
by: Peter | last post by:
Hi, A newbie question .. I want to use an array of length 4 while each array element is a string of 40 chars. I typed .. StringBuilder title = new StringBuilder(40);
3
by: Dave | last post by:
Substring returns a given string from a string. Is there an equivalent way to get a substring from a StringBuilder?
9
by: Peter Row | last post by:
Hi, I know this has been asked before, but reading the threads it is still not entirely clear. Deciding which .Replace( ) to use when. Typically if I create a string in a loop I always use a...
12
by: Richard Lewis Haggard | last post by:
I thought that the whole point of StringBuilder was that it was supposed to be a faster way of building strings than string. However, I just put together a simple little application to do a...
2
by: m00nm0nkey | last post by:
Ok well i thought i'd try a different approach, so what I'm now trying is appending 50,000 lines from the collection to a stringbuilder, and then writing that entire stringbuilder to a file. ...
34
by: raylopez99 | last post by:
StringBuilder better and faster than string for adding many strings. Look at the below. It's amazing how much faster StringBuilder is than string. The last loop below is telling: for adding...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.