StreamWriter: (Write+Flush)Async seem much slower then synchronous variants

∥☆過路亽.° 提交于 2019-12-01 01:04:27

StreamWriter, when given a file path, does not open its underlying stream in async mode. This is likely contributing to performance loss. If you're going to use it for async, you should be opening your own Stream:

Stream s = new FileStream("G:\\file.file", FileMode.Create, FileAccess.Write,
                          FileShare.None, 4096,
                          FileOptions.Asynchronous | FileOptions.SequentialScan);
StreamWriter sr = new StreamWriter(s);

Something additional to note is that your benchmark doesn't seem to capture real-world usage. Are you really writing single-character strings and flushing after each one?

Async does have a certain fixed amount of memory and GC overhead, and with such short-lived operations -- especially as StreamWriter.WriteAsync is not currently optimized for small writes -- you are bound to see much more overhead than a more common usage.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!