C# 如何确定代码的慢部分在哪里?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/485976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# How can I determine where the slow parts of my code are?
提问by
I've not be coding long so I'm not familiar with which technique is quickest so I was wondering if there was a way to do this in VS or with a 3rd party tool?
我的编码时间不长,所以我不熟悉哪种技术最快,所以我想知道是否有办法在 VS 或 3rd 方工具中做到这一点?
Thanks
谢谢
回答by Nathan W
ANTS Profileris very good.
ANTS Profiler非常好。
回答by Ric Tokyo
Tutorial/Explanationof ANTS Profiler
ANTS Profiler教程/说明
回答by jheriko
If you don't want to pay, the newer VS verions come with a profiler, but to be honest it doesn't seem very good. ATI/AMD make a free profiler... but its not very user friendly (to me, I couldn't get any useful info out of it).
如果您不想付费,较新的 VS 版本带有分析器,但说实话,它似乎不太好。ATI/AMD 制作了一个免费的分析器......但它不是很用户友好(对我来说,我无法从中获得任何有用的信息)。
The advice I would give is to time function calls yourself with code. If they are fast and you do not have a high-precision timer or the calls vary in slowness for a number of reasons (e.g. every x calls building some kind of cache), try running each one x10000 times or something, then dividing the result accordingly. This may not be perfect for some sections of code, but if you are unable to find a good, free, 3rd party solution, its pretty much what's left unless you want to pay.
我给出的建议是用代码时间函数调用自己。如果它们很快,并且您没有高精度计时器,或者由于多种原因(例如,每 x 次调用构建某种缓存),调用速度会有所不同,请尝试运行每个 x10000 次或其他次数,然后将结果除以因此。这对于某些代码部分来说可能并不完美,但是如果您无法找到好的、免费的 3rd 方解决方案,那么除非您想付费,否则剩下的就差不多了。
回答by markom
I've used ANTS Profiler and I can join the others with recommendation.
我已经使用了 ANTS Profiler,我可以通过推荐加入其他人。
The price is NEGLIGIBLE when you compare it with the amount of dev hours it will save you.
当您将其与它将为您节省的开发小时数进行比较时,价格可以忽略不计。
I you're developer for a living, and your company won't buy it for you, either change the company or buy it for yourself.
我你是开发商谋生,你公司不给你买,要么换公司,要么自己买。
回答by ng5000
For profiling large complex UI applications then you often need a set of tools and approaches. I'll outline the approach and tools I used recently on a project to improve the performance of a .Net 2.0 UI application.
为了分析大型复杂 UI 应用程序,您通常需要一组工具和方法。我将概述我最近在一个项目中使用的方法和工具来提高 .Net 2.0 UI 应用程序的性能。
First of all I interviewed users and worked through the use cases myself to come up with a list of target use cases that highlighted the systems worse performing areas. I.e. I didn't want to spend n man days optimising a feature that was hardly ever used but very slow. I would want to spend time, however, optimising a feature that was a little bit sluggish but invoked a 1000 times a day, etc.
首先,我采访了用户并亲自研究用例,以提出目标用例列表,突出显示系统性能较差的领域。即,我不想花费 n 人天来优化一个几乎从未使用过但速度很慢的功能。然而,我想花时间优化一个有点缓慢但每天调用 1000 次的功能,等等。
Once the candidate use cases were identified I instrumented my code with my own light weight logging class (I used some high performance timers and a custom logging solution because a needed sub-millisecond accuracy). You might, however, be able to get away with log4net and time stamps. The reason I instrumented code is that it is sometimes easier to read your own logs rather than the profiler's output. I needed both for a variety of reasons (e.g. measuring .Net user control layouts is not always straightforward using the profiler).
一旦确定了候选用例,我就用我自己的轻量级日志记录类来检测我的代码(我使用了一些高性能计时器和自定义日志记录解决方案,因为需要亚毫秒级的精度)。但是,您可能可以使用 log4net 和时间戳。我检测代码的原因是有时读取自己的日志比分析器的输出更容易。由于各种原因,我需要两者(例如,使用探查器测量 .Net 用户控件布局并不总是很简单)。
I then ran my instrumented code with the ANTS profiler and profiled the use case. By combining the ANTS profile and my own log files I was very quickly able to discover problems with our application.
然后我使用 ANTS 分析器运行我的检测代码并分析用例。通过结合 ANTS 配置文件和我自己的日志文件,我能够很快发现我们应用程序的问题。
We also profiled the server as well as the UI and were able to work out breakdowns for time spent in the UI, time spent on the wire, time spent on the server etc.
我们还分析了服务器和 UI,并能够计算出 UI 上花费的时间、线路上花费的时间、服务器上花费的时间等细分。
Also worth noting is that 1 run isn't enough, and the 1st run is usually worth throwing away. Let me explain: PC load, network traffic, JIT compilation status etc can all affect the time a particular operation will take. A simple strategy is to measure an operation n times (say 5), throw away the slowest and fastest run, the analyse the remianing profiles.
另外值得注意的是,1 次运行是不够的,而且 1 次运行通常值得扔掉。让我解释一下:PC 负载、网络流量、JIT 编译状态等都会影响特定操作所需的时间。一个简单的策略是测量一次操作 n 次(比如 5 次),丢弃最慢和最快的运行,分析剩余的配置文件。
回答by Mike Dunlavey
OK, downvote time...
好的,投票时间...
Profilers are great for measuring.
分析器非常适合测量。
But your question was "How can I determine where the slow parts of my code are?".
但是您的问题是“我如何确定代码的慢部分在哪里?”。
That is a different problem. It is diagnosis, not measurement.
那是一个不同的问题。它是诊断,而不是测量。
I know this is not a popular view, but it's true.
我知道这不是一个流行的观点,但这是真的。
It is like a business that is trying to cut costs.
这就像一个试图削减成本的企业。
One approach (top down) is to measure the overall finances, then break it down by categories and departments, and try to guess what could be eliminated. That is measurement.
一种方法(自上而下)是衡量整体财务状况,然后按类别和部门对其进行细分,并尝试猜测可以消除的内容。那就是测量。
Another approach (bottom up) is to walk in at random into an office, pick someone at random, and ask them what they are doing at that moment and (importantly) why, in detail.
另一种方法(自下而上)是随机走进办公室,随机挑选一个人,然后详细询问他们当时在做什么以及(重要的)原因。
Do this more than once.
这样做不止一次。
That is what Harry Truman did at the outbreak of WW2, in the US defense industry, and immediately uncovered massive fraud and waste, by visiting several sites. That is diagnosis.
这就是哈里·杜鲁门 (Harry Truman) 在二战爆发时在美国国防工业中所做的,并通过访问多个站点立即发现了大量欺诈和浪费。那就是诊断。
In code you can do this in a very simple way: "Pause" it and ask it why it is spending that particular cycle. Usually the call stack tells you why, in detail.
在代码中,您可以通过一种非常简单的方式做到这一点:“暂停”它并询问它为什么要花费那个特定的周期。通常调用堆栈会详细地告诉您原因。
Do this more than once.
这样做不止一次。
This is sampling. Some profilers sample the call stack. But then for some reason they insist on summarizing time spent in each function, inclusive and exclusive. That is like summarizing by department in business, inclusive and exclusive.
这是抽样。一些分析器对调用堆栈进行采样。但后来出于某种原因,他们坚持总结在每个功能上花费的时间,包括和排他的。这就像在业务中按部门总结,包容和排他。
It loses the information you need, which is the fine-grain detail that tells if the cycles are necessary.
它会丢失您需要的信息,这些信息是判断循环是否必要的细粒度细节。
To answer your question:
回答你的问题:
Just pause your program several times, and capture the call stack each time. If your code is very slow, the wasteful function calls will be on nearly every stack. They will point with precision to the "slow parts of your code".
只需暂停您的程序几次,并每次都捕获调用堆栈。如果您的代码很慢,那么浪费的函数调用几乎会出现在每个堆栈上。他们将精确地指向“代码的缓慢部分”。
ADDED: RedGate ANTS is getting there. It can give you cost-by-line, and it is quite spiffy. So if you're in .NET, and can spare 3 figures, and don't mind waiting around to install & learn it, it can tell you much of what your Pause key can tell you, and be much more pretty about it.
补充:RedGate ANTS 正在到达那里。它可以为您提供按行计算的成本,而且非常漂亮。因此,如果您使用 .NET,并且可以节省 3 位数,并且不介意等待安装和学习它,它可以告诉您暂停键可以告诉您的大部分内容,并且更加漂亮。
回答by Quibblesome
Eqatec profileris a cute small profiler that is free and easy to use. It probably won't come anywhere near the "wow" factor of Ants profiler in terms of features but it still is very cool IMO and worth a look.
Eqatec profiler是一款可爱的小型分析器,免费且易于使用。就功能而言,它可能不会接近 Ants profiler 的“哇”因素,但它仍然是非常酷的 IMO,值得一看。