Performace hit due to Console.WriteLine

  • Thread starter Thread starter Ashutosh
  • Start date Start date
A

Ashutosh

Hi,
If I use Console.WriteLine in a windows forms application(yes, it's not a
Console app), what does it do internally when there is no debugger attached
to the application? Will it cause any performance degrade? My application is
very heavy network based application and it writes all the messages to the
console.

I tried to test the time taken for execution of Console.WriteLine, but the
time doesn't seem significant.

I understand that Console.WriteLine should not be used in Win forms
application and it should be replaced by Debug or Trace class. But I am
facing issues in an application written by someone else (newbie) around a
year back and I can't just replace it with Debug/Trace unless there is a very
strong reason.

Please advice.
Thanks & Regards,
Ashutosh
 
Ashutosh said:
Hi,
If I use Console.WriteLine in a windows forms application(yes, it's not a
Console app), what does it do internally when there is no debugger
attached
to the application? Will it cause any performance degrade? My application
is
very heavy network based application and it writes all the messages to the
console.

I tried to test the time taken for execution of Console.WriteLine, but the
time doesn't seem significant.

I understand that Console.WriteLine should not be used in Win forms
application and it should be replaced by Debug or Trace class. But I am
facing issues in an application written by someone else (newbie) around a
year back and I can't just replace it with Debug/Trace unless there is a
very
strong reason.

Please advice.
Thanks & Regards,
Ashutosh

Set up a test where, say, 50,000 Writelines execute after some small
calculation and time the execution. Then comment out the Writeline and rerun
the test app. You'll find the Writeline takes most of the total time. You
don't say how many messages are written during a typical execution, so it's
not possible to assess the effect on your app.
 
[...]
I understand that Console.WriteLine should not be used in Win forms
application and it should be replaced by Debug or Trace class. But I am
facing issues in an application written by someone else (newbie) around a
year back and I can't just replace it with Debug/Trace unless there is a
very
strong reason.

My thinking is that if there were "a very strong reason", you wouldn't
have to ask. That is, you'd actually _see_ a problem with the application
as delivered. If there are no complaints related to the use of the
console/stdout, then there's no problem and no strong reason to change it.

Pete
 
Hi,
The application I am talking about is having lots of performance related
issues. And I think that writing every trace message to Console does add to
the performance degrade. The application receives 10-15 packets(or more) per
seconds from the network and write information(the information contains a lot
of data) about it to the console. Under heavy load the number can be much
much higher. So, the number of writes can be much higher.

Let me change the question slightly, if there is a WinForms application
which writes to console using Console.WriteLine, then in the release version
of the application when there are no debugger attached to the process, what
does the call to Console.WriteLine does internally? Does it do some
processing internally?

Regards,
Ashutosh



Peter Duniho said:
[...]
I understand that Console.WriteLine should not be used in Win forms
application and it should be replaced by Debug or Trace class. But I am
facing issues in an application written by someone else (newbie) around a
year back and I can't just replace it with Debug/Trace unless there is a
very
strong reason.

My thinking is that if there were "a very strong reason", you wouldn't
have to ask. That is, you'd actually _see_ a problem with the application
as delivered. If there are no complaints related to the use of the
console/stdout, then there's no problem and no strong reason to change it.

Pete
 
Hi,
Sorry, this is not what I am looking for. I have already done those tests. I
don't have to test with so many rows. I need to test only 20-50 lines at a
time and under my test the time taken were close to 0 milli-seconds. That's
the reason I posted the question here. Hope u understand.

Regards,
Ashutosh
 
"Ashutosh" <[email protected]> az alábbiakat írta a
következő üzenetben
Hi,
The application I am talking about is having lots of performance related
issues. And I think that writing every trace message to Console does add
to
the performance degrade. The application receives 10-15 packets(or more)
per
seconds from the network and write information(the information contains a
lot
of data) about it to the console. Under heavy load the number can be much
much higher. So, the number of writes can be much higher.

Let me change the question slightly, if there is a WinForms application
which writes to console using Console.WriteLine, then in the release
version
of the application when there are no debugger attached to the process,
what
does the call to Console.WriteLine does internally? Does it do some
processing internally?

Regards,
Ashutosh

Hi ,

Actually what WriteLine does is writing to the TextWriter designated as the
out writer. The Console class has 3 writer/ reader attached to it : 1 for
output, 1 for errors, and 1 for input. When You run a console application
these are bound to the console window by default, however You can always
redirect those by calling the appropriate methods on the Console class.
So if You wish You can redirect all the lines written by Console.WriteLine
to anywhere You want, given You write some kind of adapter. Check out this
page for more info & example:
http://msdn.microsoft.com/en-us/library/system.console.setout.aspx

Hope You find this useful.
-Zsolt
 
Hi Miher,
Thanks for the information, but I am already aware of those things. What I
am asking is for a Windows Application, not console application. I want to
know if the console output is not re-directed then what happens internally?
Is the statement is ignored during execution or the run time write the data
to some stream?

Thanks & Regards,
Ashutosh
 
Ashutosh said:
Hi Miher,
Thanks for the information, but I am already aware of those things. What I
am asking is for a Windows Application, not console application. I want to
know if the console output is not re-directed then what happens internally?
Is the statement is ignored during execution or the run time write the data
to some stream?

Thanks & Regards,
Ashutosh

If I understand your goal, then I don't think there is any difference in
the resulting code whether Console.Writeline("HI!"); comes from a button
click event in a windows form, or from a console app. Looking at the
codes for these in reflector looks identical.
 
Hi,
You are partially correct. ILDasam and reflecor will show the same code. But
the difference in winForm and console app is that console app has a *VISIBLE*
output/input/error streams attached. However, in winForm application it's not
visible.

Regards,
Ashutosh
 
Ashutosh said:
Hi,
You are partially correct. ILDasam and reflecor will show the same code. But
the difference in winForm and console app is that console app has a *VISIBLE*
output/input/error streams attached. However, in winForm application it's not
visible.

Regards,
Ashutosh

Create a console app that does a showdialog on the form that I
described. The console output goes to the console and the code for the
button is still the same. I don't know any other way of showing the
code for a console write is the same regardless of the platform.
 
Ashutosh said:
Hi Miher,
Thanks for the information, but I am already aware of those things. What I
am asking is for a Windows Application, not console application. I want to
know if the console output is not re-directed then what happens
internally?
Is the statement is ignored during execution or the run time write the
data
to some stream?

Thanks & Regards,
Ashutosh


Hi,
As i know if nothing is attached to the application (no console window, no
debugger) then it writes to NullStream (what is a null object pattern
implementation of Stream) what does nothing.

-Zsolt
 
Hi,
If I use Console.WriteLine in a windows forms application(yes, it's
not a Console app), what does it do internally when there is no
debugger attached to the application? Will it cause any performance
degrade? My application is very heavy network based application and it
writes all the messages to the console.

I tried to test the time taken for execution of Console.WriteLine, but
the time doesn't seem significant.

I understand that Console.WriteLine should not be used in Win forms
application and it should be replaced by Debug or Trace class. But I
am facing issues in an application written by someone else (newbie)
around a year back and I can't just replace it with Debug/Trace unless
there is a very strong reason.


As it stands, the overhead of writing to Console, even in a Windows
Forms app, is negligible. There is a console out, even if it is not
console.

The only "potential" danger I can see (brainstorming here) is if you
were to fire this up with a Process object and try to read from out,
which is unlikely since this is a .NET object.

BUT, considering the overhead to moving to Trace or Debug is

Find: Console.WriteLine
Replace: Debug.WriteLine

and adding a few namespace references, I think the case can easily be
made to refactor this. Crappy code is crappy code, whether or not it is
affecting performance. Just my two cents.



--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 
Back
Top