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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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..   ?
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 ;)


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.