Was profiling my code and found that the way we are doing colored console text is very expensive (majority of the runtime).
DateTime dt = DateTime.Now;
for (int i = 0; i <= 20000; i++)
{
ConsoleColor cf = Console.ForegroundColor;
ConsoleColor cb = Console.BackgroundColor;
Console.ForegroundColor = ConsoleColor.Red;
Console.BackgroundColor = ConsoleColor.Blue;
Console.WriteLine("Hello World");
Console.ForegroundColor = cf;
Console.BackgroundColor = cb;
}
System.Diagnostics.Debug.WriteLine((DateTime.Now - dt).TotalMilliseconds);
This simple loop takes 2.8 seconds to run on my machine. If I just do the WriteLine, its only 600ms.
Now, before I get troll answers :) asking why I keep setting the color when its hardcoded, THIS IS TEST CODE, in the real code, the foreground and background colors are calculated on a few different factors. That part is irrelevant. This code is just assuming that the color will change and thus saves the originals, changes the colors, then changes it back to the original.
I've also tried using the native SetConsoleTextAttribute method through pinvoke since using ILSpy, it seemed like the Console.xxx methods were doing a lot of extra crap, but I got about the same timings.