System.Console is slow

Reading time ~1 minute

This post is written more as a warning

Anyone who made any HackerRank problems considering performance has seen this phrase in the assignment: “watch out for slow IO”.
We are used to think about files, databases and such as potentially slow IO, but the Console?
Yes, and you will be amazed how much.

Couple words about the setup.
I am using NLog with file target (for normal logging) and mail target (for total failure, and aggregated reports).
When debugging or profiling I run the process as a console application, so I have a NLog console target. It outputs some basic info and debug messages summing up to exactly 329KB of text.

So here are the numbers of running the application with console target:

And here are the numbers with console target commented:

28 seconds for outputting 329 KB of text.
To be fair, if you output it in one big chunk it will be faster.

To sum it up:

  • don’t assume something is fast. Measure it!
  • control your output. If this was a program running normally as a console application with Console.WriteLine thrown all over the code base the change wouldn’t take less than 1 minute.
  • NLog is a good abstraction over where your logs go.

Hi, I'm Szymon Warda. I write code, design IT systems, write this blog, tweet and speak at conferences. If You want to know more go here, or follow me: