Console.WriteLine加快了我的代码? [英] Console.WriteLine speeds up my code?
问题描述
我一直在寻找到加快我的应用程序,因为它是性能的关键......即每毫秒我可以摆脱它更好。要做到这一点,我有一个调用其他方法和每一种其他的方法是包裹着一个秒表
定时器的方法和 Console.WriteLine
来电。即:
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);
//...
}
问题是每当我注释掉秒表
和 Console.WriteLine
行代码运行约20毫秒(不是50)慢这是一个?很多关于我需要什么
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.
有谁知道这是为什么。
编辑:
中的 SomeMainMethod
方法和其他类也被包裹在一个秒表
和 Console.WriteLine
调用类似于上面。
The SomeMainMethod
method and others in the class are also wrapped in a Stopwatch
and Console.WriteLine
calls similar to above.
SomeMainMethod
和。它调用的方法是一个类,是一个类库是从一个控制台测试平台调用,所有这一切是单线程的一部分的一部分
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.
有关详细信息:应用程序在x86的.NET 4.6.1发布模式与运行的优化功能。我也是在Visual Studio 2013不被外部的运行它这一点。
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.
推荐答案
阅读一个非常类似的质疑与没有答案我可能已经找到了问题。在评论部分用户(ForguesR)做了如下评论:
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:
这实在是一个大猜想:也许是因为你写IO你的线程得到更多的处理器时间,因为的WriteLine是同步的,从而阻止其他线程。
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.
所以,我想检查,如果这是事实确实如此所以我改变 SomeMainMethod
喜欢以下内容:
So I wanted to check if this was indeed the case so I changed SomeMainMethod
to like the following:
注意:一般不建议玩弄线程优先级,这只是测试理论解决方法。我会强烈建议不要除非你是100%肯定,你知道你在做什么,在生产代码中这样做。那么很可能仍远离它。
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;
}
}
让现在这个更改我的代码运行后始终快( 〜10ms)的当控制台
和秒表
行注释掉。因此,我相信他的评论是可能是正确的,在我的情况最少。
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.
这篇关于Console.WriteLine加快了我的代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!