How I get the best performance out of .NET's StreamWriter in C#?

Carlos Castillo picture Carlos Castillo · Aug 13, 2009 · Viewed 7.7k times · Source

What is the recommended approach to get the best performance when we need to create text files bigger than 10 MB?

There are multiple sections in the code that need to write stuff to a single file. This means a lot of text lines.

Option #1 (This logic will be called at several times):

  1. Create a StreamWriter instance
  2. Write some lines (according to some business logic)
  3. Close the StreamWriter instance

Option #2:

  1. Create a StreamWriter at the begining of the program
  2. Write all of the lines from diferent sections of the code.
  3. Close the StreamWriter at the very end when nothing else need to be written.

Option #3: Any other?

Remember the output file could be bigger than 10 MB.

Answer

Jon Skeet picture Jon Skeet · Aug 13, 2009

Holding a single writer open will be more efficient than repeatedly opening and closing it. If this is critical data, however, you should call Flush() after each write to make sure it gets to disk.

Is your program multi-threaded? If so, you may wish to have a producer/consumer queue - have a single thread fetching items to write from the queue and writing them, then other threads can freely put items on the queue.

Are you sure you actually have a performance problem though? 10MB is pretty small these days... on my netbook it still only takes about a second or two to write 10MB (and no, that's not a solid state drive).