So I have noticed this while using a few differnt languages on Windows XP, Vista and 7. If you dump a couple thousand lines a second to the console window it generally has a negative impact on the performance of the system. Computer are fast, and always faster, and the console looks like just some text on a back ground. Where is the bottle neck?
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.
When we are directly writing outputs to our terminal, each writing operation is being done “synchronously”, which means our programs waits for the “write” to complete before it continues to the next commands. Each time our programs writes something to stdout , we are met with this delay.
Writing to the console is faster, but if the write operation is all that's performed, then a program writing to a file will probably finish first.
printf etc has to go from your process with some sort of IPC to the Console window process (csrss or conhost) The console can also be shared with several processes so there is also some synchronization and buffering going on.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With