Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Console.Writeline Effect on Performance

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 their logs by using Console.Writeline. The performance is very important in this application. I wanted to know whether removing of Console.Writeline will tune the performance of this application or not?

like image 429
Pooya Yazdani Avatar asked Aug 27 '13 12:08

Pooya Yazdani


3 Answers

Yes, executing Console.WriteLine takes a measureable amount of time.

Removing the Console.WriteLine call or changing it to a buffered background thread writing the data would really speed up the application.

However your milage might vary depending on the OS in use.

like image 122
CodingBarfield Avatar answered Nov 12 '22 10:11

CodingBarfield


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.

like image 31
Paolo Falabella Avatar answered Nov 12 '22 10:11

Paolo Falabella


If it's for debugging purpose, you should rather use: Debug.WriteLine(..), because these are not included in the release version.

like image 34
Jeroen van Langen Avatar answered Nov 12 '22 08:11

Jeroen van Langen