Debug.WriteLine() and/or Console.WriteLine - expensive to use many times?

Can anyone tell me...

How expensive(processing, or speed) is it to use many Debug.WriteLine()  or Console.WriteLine()  functions throughout your code?   I currently have many, many of these all throughout my app and was wondering how much efficiency i may gain if i comment them all out?

Who is Participating?
The DEBUG symbol is defined in the Debug build configuration properties by default, so commenting out a #define at the top of the file will not necessarily get rid of it if you are doing a DEBUG build. As sumix says, Debug.WriteLine acts in a similar way, though I like to use #if in combination with build configurations that have other symbols defined so that I can debug with/without comments etc. E.g. I have TEST build with the corresponding symbol which I use in conjuction with NUnit to do unit testing, which might require extra console information to be written.

So your problem is probably that you are building under a debug build configuration in Visual Studio. Try switching between a debug and release configuration and see if there is a difference.

Just as a note, sumix and lblinc, caching and writing my info lines out 100 at a time made a significant improvement in a piece of code that was building a 15 GB database and was taking 2.3 seconds per transaction over two days with several status messages per transaction. Removing the console writes altogether and meaning I didn't have to maintain a live progress GUI for the production code made even more of a difference. Knocking even fractions of time out of that project was very important. This is maybe a little unusual though ;)

It does slow down an application. You can easily see the effect by writing a simple console app to count to a million, and do it with and without writing each integer to the console. You could consider caching the output and only writing blocks of 10, 100, 1000 ... lines. Alternatively if you only need them for debugging, surround them with a preprocessor directive such as:


Alternatively, put the Console.WriteLine call into your own function, and then you can easily turn it on/off to see the effect, or use the application's config file to turn it on/off.


 If you use Debug class you don't need to modify the code at all, building the application without DEBUG directive will exclude all Debug.WriteLine statements.
 Second, how much a delay of a few seconds does disturb you? Do you really have Writeline statements executed hundreds of thousands times?

  Using Debug and Trace does help a lot, and, as MSDN documentation says:
  "If you use methods in the Debug class to print debugging information and check your logic with assertions, you can make your code more robust without impacting the performance and code size of your shipping product."

lblincAuthor Commented:
Any thought on this?  

It seems that when i use the DEBUG directive..  i.e.   #define DEBUG    and i comment it out like this...

//#define DEBUG

it still seems to be still writing out the Debug.WriteLines to the debug window.    Is this correct?

i thought that when you comment out the #define  that it meant the  #if DEBUG  statement would not be called..   ?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.