Console.Writeline in release builds a performance issue ?

  • Thread starter Thread starter Sagaert Johan
  • Start date Start date
The short answer is I don't know. But why dont you try looping over 1
million times doing Console.Writeline and see how long it takes. If it
takes 1 second or less (dont run it under debug mode) then theres really
no point going round commenting out all the references in your code.

Instead, why not define a DEBUG symbol and only include it in debug
builds. eg
#if DEBUG
Console.Writeline("This is a debug only statement");
 
I couldn't help myself...

DateTime start = DateTime.Now;
for(int i=0;i<1000000;i++)
Console.WriteLine("hello there");

StreamWriter writer = new StreamWriter("time.txt");
string line = "time=" + (DateTime.Now.Ticks-start.Ticks)/10000 +
"ms";
writer.WriteLine(line);
writer.Close();

Takes about 500ms on my machine (2.8Ghz nothing special)
 
An easier way rather than putting lots of #if DEBUG all over your code is to
use either the:

System.Diagnostics.Debug.WriteLine or
System.Diagnostics.Trace.WriteLine methods

When in debug mode both Trace and Debug will output, when in release only
trace will output, unless you remove the TRACE symbol from you compiler
options.

Hope that helps.
Mark R Dawson
http://www.markdawson.org
 
Thanks



Mark R. Dawson said:
An easier way rather than putting lots of #if DEBUG all over your code is to
use either the:

System.Diagnostics.Debug.WriteLine or
System.Diagnostics.Trace.WriteLine methods

When in debug mode both Trace and Debug will output, when in release only
trace will output, unless you remove the TRACE symbol from you compiler
options.

Hope that helps.
Mark R Dawson
http://www.markdawson.org
 
Sagaert said:
Can a lot of Console.Writeline commands cause a program to run slower

Compared to what? Not doing something will always be faster than doing
something.
 
Back
Top