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):
Option #2:
Option #3: Any other?
Remember the output file could be bigger than 10 MB.
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).