Console.WriteLine slow

后端 未结 9 1580
天涯浪人
天涯浪人 2020-11-29 07:35

I run through millions of records and sometimes I have to debug using Console.WriteLine to see what is going on.

However, Console.WriteLine

9条回答
  •  旧巷少年郎
    2020-11-29 07:51

    Why Console is slow:

    • Console output is actually an IO stream that's managed by your operating system. Most IO classes (like FileStream) have async methods but the Console class was never updated so it always blocks the thread when writing.

    • Console.WriteLine is backed by SyncTextWriter which uses a global lock to prevent multiple threads from writing partial lines. This is a major bottleneck that forces all threads to wait for each other to finish the write.

    • If the console window is visible on screen then there can be significant slowdown because the window needs to be redrawn before the console output is considered flushed.

    Solutions:

    Wrap the Console stream with a StreamWriter and then use async methods:

    var sw = new StreamWriter(Console.OpenStandardOutput());
    await sw.WriteLineAsync("...");
    

    You can also set a larger buffer if you need to use sync methods. The call will occasionally block when the buffer gets full and is flushed to the stream.

    var sw = new StreamWriter(Console.OpenStandardOutput(), Encoding.UTF8, 8192);
    sw.Write("...") // this will block for flushing when the buffer size of 8192 is full
    

    If you want the fastest writes though, you'll need to make your own buffer class that writes to memory and flushes to the console asynchronously in the background using a single thread without locking. The new Channel class in .NET Core 2.1 makes this simple and fast. Plenty of other questions showing that code but comment if you need tips.

提交回复
热议问题