I have a console application with lots of WriteLine()
functions, this application should also run as a "service" (a scheduled task, the user is not logged in when it runs).
In this case, there is no actual cmd window to show. Will Console.WriteLine()
still cause a noticeable performance penalty in such a case?
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.
Console. WriteLine() ) is a sequential blocking call and under load that blocking is enough to seriously slow down request processing.
WriteLine(String, Object, Object) Writes the text representation of the specified objects, followed by the current line terminator, to the standard output stream using the specified format information.
The first solution is to run the application without debugging by using Ctrl+F5 instead of just F5. The console window will remain open when the program has finished.
Yes it will cause performance penalty even when the console is not visible
my experiment was running this piece of code with the console visible and again with the console hidden and measure the performance
first run Console Visible 24683 Millisecond
second run Console Visible 23363 Millisecond
first run Console Hidden 167 Millisecond
second run Console Hidden 162 Millisecond
static void Main(string[] args)
{
WriteMilionLines();
}
static void WriteMilionLines()
{
Stopwatch sw = new Stopwatch();
sw.Start();
for (int i = 0; i < 1000000; i++)
{
Console.WriteLine(i);
}
sw.Stop();
string message = "ElapsedMilliseconds" + sw.ElapsedMilliseconds;
File.WriteAllText(@"c:\log.txt", message);
}
By commenting Console.WriteLine(i);
line it will take Zero millisec
No, it won't be noticeable. Unless your application does 99% Console.WriteLine()
and barely anything else, the difference will be negligible.
If you still worry about performance, you could wrap Console-Calls into a method that you have control over and measure the total time your application went through and even introduce a switch (bool variable) to control its calls.
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