I have been looking into speeding up my application as it is performance critical... i.e. every millisecond I can get out of it is better. To do this I have a method that calls some other methods and each of these other methods is wrapped with a Stopwatch
timer and Console.WriteLine
calls. I.e.:
private void SomeMainMethod()
{
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();
SomeMethod();
sw.Stop();
Console.WriteLine("Time for SomeMethod = {0}ms", sw.ElapsedMilliseconds);
sw.Reset();
sw.Start();
SomeOtherMethod();
sw.Stop();
Console.WriteLine("Time for SomeOtherMethod= {0}ms", sw.ElapsedMilliseconds);
//...
}
The problem is whenever I comment out the Stopwatch
and Console.WriteLine
lines the code runs about 20ms (not 50) slower which is a lot for what I need.
Does anyone know why this is?
EDIT:
The SomeMainMethod
method and others in the class are also wrapped in a Stopwatch
and Console.WriteLine
calls similar to above.
The SomeMainMethod
and the methods it calls is part of a class that is part of a Class Library that is called from a console testbed, all of which is single threaded.
For more information: The app is running in x86 .NET 4.6.1 Release mode with optimisations enabled. I am also running this in visual studio 2013 not outside of it.
These C# example programs show the Console.WriteLine method. Console.WriteLine has many overloads. Console.WriteLine renders a line of text on the console.
It is helpful to know that when you use the Console class, you do not need to create a new Console (). You need to add the "using System" line at the start. WriteLine is a static method on the Console type. Alternatively: You can specify System.Console.WriteLine () to reference the namespace directly. Empty line.
Console.WriteLine ( "B" ); } } Output A B Format strings. In some programs, you will want to write several values on a single line to the Console. One way you can do this is by appending the string using the + operator before passing it to Console.WriteLine. It is often clearer to use a format string.
Please remove Console.Readline () or use System.Console.ReadLine (); to halt the screen. Now all your console.Witeline output will be executed as debug.WriteLine and console output visible in Output window.
After reading a very similar question with no answers I may have found the issue. In the comments section a user (ForguesR) made the following comment:
It is really a big guess : maybe because you are writing to IO your thread gets more processor time because WriteLine is synchronized and thus blocking other threads.
So I wanted to check if this was indeed the case so I changed SomeMainMethod
to like the following:
NOTE: It is generally not advised to play around with thread priorities, this was only a workaround to test the theory. I would strongly advise against doing this in production code unless you are 100% sure you know what you are doing. Then probably still stay away from it.
private void SomeMainMethod()
{
System.Threading.ThreadPriority tp = System.Threading.ThreadPriority.Normal;
try
{
tp = System.Threading.Thread.CurrentThread.Priority;
System.Threading.Thread.CurrentThread.Priority = System.Threading.ThreadPriority.Highest;
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();
SomeMethod();
sw.Stop();
Console.WriteLine("Time for SomeMethod = {0}ms", sw.ElapsedMilliseconds);
sw.Reset();
sw.Start();
SomeOtherMethod();
sw.Stop();
Console.WriteLine("Time for SomeOtherMethod= {0}ms", sw.ElapsedMilliseconds);
//...
}
finally
{
System.Threading.Thread.CurrentThread.Priority = tp;
}
}
After making this change my code now runs consistently faster (~10ms) when the Console
and Stopwatch
lines are commented out. Therefore I believe his comment was probably correct, at least in my situation.
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