Performance problems of StreamWriter

º

ºa¤Ö

Dear All,

Which performance is better? I think 2 is better because only 1 I/O is
performed. Am I right?

1.
using (StreamWriter sw = File.Append(FileName))
{
sw.WriteLine(text1);
sw.WriteLine(text2);
sw.WriteLine(text3);
sw.WriteLine(text4);
sw.WriteLine(text5);
}

2.
using (StreamWriter sw = File.Append(FileName))
{
StringBuilder sb = new StringBuilder();
sb.Append(text1);
sb.Append(text2);
sb.Append(text3);
sb.Append(text4);
sb.Append(text5);

sw.WriteLine (sb.ToString());
}
 
P

Peter Duniho

Dear All,
Which performance is better? I think 2 is better because only 1 I/O
is performed. Am I right?

Well, for one, the two pieces of code don't do the same thing. You
left out the NewLine character in the second version.

As far as whether one performs better than the other, I don't know.
You'd have to look carefully at the implementation of the underlying
stream. If it does some buffering, or if it handles the NewLine
characters in some special way when given a complete string with
multiple NewLine characters, or some other factor, it could be that
individual calls to WriteLine are either no slower than a single one
preceded by assembling a single string with a StringBuilder, or they
could even be faster in some theoretical world.

Personally, seems like a waste of time to think about it, at least
without having some specific reason to. Which one is more
maintainable in the context in which you're doing the operation? I
doubt there's a significant performance difference either way and
absent any real-world evidence that there is, you should instead focus
on making the code _work_, and be maintainable, and not worry so much
about some subtle performance difference.

Pete
 
J

Jon Skeet [C# MVP]

ºa¤Ö said:
Which performance is better? I think 2 is better because only 1 I/O is
performed. Am I right?

1.
using (StreamWriter sw = File.Append(FileName))
{
sw.WriteLine(text1);
sw.WriteLine(text2);
sw.WriteLine(text3);
sw.WriteLine(text4);
sw.WriteLine(text5);
}

2.
using (StreamWriter sw = File.Append(FileName))
{
StringBuilder sb = new StringBuilder();
sb.Append(text1);
sb.Append(text2);
sb.Append(text3);
sb.Append(text4);
sb.Append(text5);

sw.WriteLine (sb.ToString());
}

Have you measured it? Do you *actually* have a problem, or are you just
guessing that you might?

I believe StreamWriter does buffering anyway, but either way I would
use the most straightforward code until I'd seen an actual performance
issue.
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

ºa¤Ö said:
Which performance is better? I think 2 is better because only 1 I/O is
performed. Am I right?

1.
using (StreamWriter sw = File.Append(FileName))
{
sw.WriteLine(text1);
sw.WriteLine(text2);
sw.WriteLine(text3);
sw.WriteLine(text4);
sw.WriteLine(text5);
}

2.
using (StreamWriter sw = File.Append(FileName))
{
StringBuilder sb = new StringBuilder();
sb.Append(text1);
sb.Append(text2);
sb.Append(text3);
sb.Append(text4);
sb.Append(text5);

sw.WriteLine (sb.ToString());
}

#2 is a good example of performance tuning when it is worst.

Difficult to read code (you forgot the newlines yourself - what do you
think the maintenance programmer will do?).

Code that actually *decreases* performance.

Stick with #1.

And if you need to optimize it for writing large files, then
use the possibilities provided in the framework library - there
are StreamWriter constructors that take a buffer size argument -
specifying a huge buffer size actually increase performance when
writing large files.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top