问题描述
好像世上没有免费的* .NET性能分析器,可以一行接一行的基础资料。因此,我期待到使用秒表进行分析。
It seems there are no free* .NET performance profilers that can profile on a line-by-line basis. Therefore, I am looking into using Stopwatch for profiling.
*自由的自由,即许可包括商业应用。
*free as in freedom, i.e. license includes commercial applications.
编辑:在回答那些谁告诉我:买一个分析器,我想,但如果我能花那么多钱,我将钱花在别的东西。我试图说服我的老板,一个分析器是值得的,但还没有多少运气。这个问题主要是基于好奇心。我绝不会考虑秒表作为替换为一个真正的分析器。
In response to those who told me "buy a profiler", I would like to, but if I could spend that much money I would spend it on something else. I tried to convince my boss that a profiler is worth it, but haven't had much luck. This question is mostly based on curiosity. I would never consider Stopwatch as a replacement for a real profiler.
我有一个小的测试应用程序(C#编写的),其在每个线的基础上使用秒表测量时的性能差异。测试code是这样的:
I have a little test app (written in C#) that measures performance differences when using a Stopwatch on a per-line basis. The test code is this:
int n = 100;
BigInteger f = 1;
for (int i = n; i > 1; i--)
{
f *= i;
}
下面是完整的code: http://pastebin.com/AvbQmT32
Here is the full code: http://pastebin.com/AvbQmT32
我有一个秒表为code每一行。这是我的探查。我也有一个秒表整个程序。这是我的'探查探查。
I have a Stopwatch for each line of code. This is my 'profiler'. I also have one Stopwatch for the whole program. This is my 'profiler profiler'.
我已经配置为发布模式,任何CPU(在x64机器上)的程序,优化禁用。
I have the program configured as Release mode, Any CPU (on an x64 machine), and optimizations disabled.
当我运行程序与禁用探查,我得到的是这样的:
When I run the program with the profiler disabled, I get something like this:
Line | Ticks
------------------------------|----------
|
Total time: | 359
当我在启用分析器运行它,我得到的是这样的:
When I run it with the profiler enabled, I get something like this:
Line | Ticks
------------------------------|----------
|
int n = 100; | 3
BigInteger f = 1; | 12
for (int i = n; i > 1; i--) | 325
{ |
f *= i; | 539
} |
|
Total time: | 1710
Stopwatch overhead: | 831
理想情况下,花费在code中的时间应等于在这两种情况下,但现在看来,在秒表有自己的经过时间内出现的开销。
Ideally, the time spent on code should be equal in both cases, but it appears that the Stopwatches have overhead that appears within their own elapsed time.
现在,它没有通常意义的需要来分析程序的每一行,因为它通常工作更好地与分而治之的办法。您通常可以分析的code块入手,缩小任何性能问题。
Now, it doesn't often make sense to need to profile every line of a program, as it usually works better with a divide-and-conquer approach. You can usually start by profiling chunks of code, and narrow down on any performance problems.
另外,在大多数应用中,code中的平均线将是比那些在测试程序中慢很多。这意味着将有更少秒表开销。
Also, in most applications, the average line of code will be a lot slower than the ones in the test program. This means that there will be less Stopwatch overhead.
不过,在使用秒表时,特别是如果你使用了大量的仍然是开销。
However, there is still overhead when using Stopwatches, especially if you use a lot.
所以到了一个问题:
什么是用秒表进行分析的最有效方法是什么?我怎样才能最大限度地减少开销?它甚至值得环绕一个语句秒表?
What is the most effective way to use Stopwatches for profiling? How can I minimize overhead? Is it even worth it to wrap a Stopwatch around a single statement?
我AP preciate您的反馈意见。
I appreciate your feedback.
推荐答案
首先要做的第一:你的结果我并不感到惊讶。如果你使用一个商业事件探查器,你会看到类似的东西:你的程序将需要更长的时间,当它正在分析比当它不运行。在更细化的设置探查,时间越长,你能指望它拿。当你考虑到喜欢的语句:我> 1和我 - 很可能会为单一处理器的说明,可以很明显的原因分析特定线路的运行时间可以采取很多长于执行线本身。执行
First thing's first: your results are not at all surprising. If you used a commercial profiler, you would see something similar: your program will take a lot longer to run when it is being profiled than when it is not. The more granular you set the profiler, the longer you can expect it to take. When you consider that statements like "i > 1" and "i--" will likely be executed as single processor instructions, it becomes obvious why profiling the execution time of a particular line can take a lot longer than executing the line itself.
这剖析增加程序的总运行时间不应该是一个问题的事实;几个其他人所说,重要的不是的程序运行时的绝对数额,但比对彼此的各个部分的运行时间来寻找瓶颈。但还有另一种担忧。秒表将使用高频计时器从底层的操作系统(如果有);但即使这样也不够高。在我的Windows 7 64位i5-2400(四核3.10 GHz)的高频时钟嘀哒每秒3020556次。这听起来像一个不少;但这样的速度,处理器可以执行刻度之间一千指令。这意味着,如果你试图测量它需要执行一个指令时,你要么在或者方式冲去的方式。
The fact that profiling is increasing the overall running time of your program should not be a concern; as several others have mentioned, what matters is not the absolute amount of time the program runs, but comparing the running time of the individual parts against each other to find the bottleneck. But there is another concern. Stopwatch will use the high frequency timer from the underlying OS if one is available; but even that may not be high enough. The high frequency timer on my Windows 7 64-bit i5-2400 (Quad core 3.10 GHz) ticks 3,020,556 times per second. That sounds like a lot; but at that rate, the processor could execute a thousand instructions between ticks. That means if you are trying to measure the time it takes to execute a single instruction, you are either going to way under or way overshoot.
您会更好地分析在方法级别。即使是这样,你会遇到频率的问题,特别是如果你有小的良好分解的方法。但结果会比在线路级别更可靠;一旦你已经确定,是造成瓶颈的方法,你可以直接检查它来确定如何优化它有更好的表现。
You would be better off profiling at the method level. Even then you will run into frequency issues, particularly if you have small well-factored methods. But the results will be more reliable than at the line level; and once you have identified the method that is causing the bottleneck, you can examine it directly to determine how to optimize it to perform better.
这一切都撇开了许多注意事项与一般的性能分析,这将是这个职位的范围之内走。请确保你做更多的研究对象,了解你应该如何跨preT什么结果,你得到的。作为一个简单的例子,你的分析可能显示,大多数在你的程序中的时间被消耗在一个特定的方法;但这是否意味着,该方法本身的问题,或者其他方法调用它经常?回答那样的问题,就是在分析真正的困难所在。
All of this leaves aside the many caveats that go along with performance profiling in general, which would be outside the scope of this post. Make sure you do additional research on the subject to understand how you should interpret whatever results you get. As a quick example, your profiling may reveal that the majority of the time in your program is being spent in a particular method; but does that mean that the method itself is the problem, or that other methods are calling it to often? Answering that kind of question is where the real difficulty in profiling lies.
这篇关于剖析.NET应用程序与秒表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!