6

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.

4

4 回答 4

16

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).

于 2009-08-13T17:42:54.597 回答
0

在场景 1 和 2 中,您必须问自己是否需要同时访问文件。在这种情况下,场景 2 StreamWriter 不是一个选项,因为它没有同步。在场景 1 中,您应该打开每个 StreamWriter,使其获得文件的排他锁。

假设顺序访问,我永远不会使用方案 2。它需要将 StreamWriter 传递给需要它的每个代码部分。谁负责再次关闭作家。这将很快变得无法维护。

场景 1 的缺点是您必须在任何需要的地方打开 StreamWriter,这也变得不可维护。此外,现在您必须知道每个代码段中文件的位置。

我会在 StreamWriter 周围使用单例包装器,这样您就可以在任何您喜欢的地方使用它,而不会在 StreamWriter 本身上创建很多依赖项。

于 2009-08-13T17:53:13.497 回答
0

只需混合两种方法......使用一个缓冲区,允许您在内存中存储尽可能多的内容。一旦超过该大小,您的缓冲区将被写入磁盘并清理。

于 2009-08-13T17:53:29.690 回答
0

Use a StringBuilder to concatenate your text and only open and write once in the file.

于 2009-08-13T17:43:31.820 回答