Console.Writeline Effect on Performance

前端 未结 3 547
庸人自扰
庸人自扰 2021-01-02 04:53

I have an application that has 4 threads. Each thread is actually a Timer and does a seperate job in specific intervals. These threads show

3条回答
  •  被撕碎了的回忆
    2021-01-02 05:18

    There may be two issues with Console.WriteLine in regards to performance:

    1. IO is not typically a "fast" operation.

    2. Calls to WriteLine are synchronized, i.e. if two threads want to write, one of them blocks on the WriteLine waiting for the other to finish writing. From MSDN on console:

    I/O operations that use these streams are synchronized, which means that multiple threads can read from, or write to, the streams.

    That said, the only way to understand if the time spent on Console.WriteLine has an impact on the performance of your specific application is profiling it. Otherwise it's premature optimization.

提交回复
热议问题