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.