C# 如何确定代码中速度较慢的部分在哪里?

发布于 2024-07-13 13:59:58 字数 70 浏览 4 评论 0原文

我编码时间不长,所以我不熟悉哪种技术最快,所以我想知道是否有办法在 VS 或第三方工具中做到这一点?

谢谢

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?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

帅哥哥的热头脑 2024-07-20 13:59:58

分析仪非常适合测量。

但你的问题是“我如何确定代码中速度较慢的部分在哪里?”。

那是一个不同的问题。 这是诊断,而不是测量。

我知道这不是一个流行的观点,但这是事实。

这就像一个试图削减成本的企业。

一种方法(自上而下)是衡量总体财务状况,然后按类别和部门进行细分,并尝试猜测可以消除哪些内容。 这就是衡量。

另一种方法(自下而上)是随机走进办公室,随机挑选某人,详细询问他们当时在做什么以及(重要的是)为什么。

不止一次这样做。

这就是哈里·杜鲁门在二战爆发时在美国国防工业中所做的事情,并通过访问多个网站立即发现了大规模的欺诈和浪费。 这就是诊断。

在代码中,您可以通过一种非常简单的方式来完成此操作:“暂停”它并询问它为什么要花费该特定周期。 通常调用堆栈会详细告诉您原因。

多次执行此操作。

这就是采样。 一些分析器会对调用堆栈进行采样。 但出于某种原因,他们坚持总结在每个功能上花费的时间,包括包容性和排他性。 这就像业务中按部门总结一样,有包容性,也有排他性。

它会丢失您需要的信息,这些信息是告诉循环是否必要的细粒度细节。

回答您的问题:

只需暂停程序几次,然后每次捕获调用堆栈即可。 如果您的代码非常慢,则几乎每个堆栈都会出现浪费的函数调用。 他们将精确地指出“代码中缓慢的部分”。

补充:RedGate ANTS 即将实现这一目标。 它可以给你按行列出的成本,而且非常漂亮。 因此,如果您使用 .NET,并且可以节省 3 位数,并且不介意等待安装和安装。 学习它,它可以告诉您暂停键可以告诉您的很多信息,并且更加漂亮。

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.

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.

套路撩心 2024-07-20 13:59:58

分析。

RedGate 有一个产品。
JetBrains 有一个产品。

Profiling.

RedGate has a product.
JetBrains has a product.

笑梦风尘 2024-07-20 13:59:58

我已经使用了 ANTS Profiler,我可以加入其他人的推荐。

当你将它与它为你节省的开发时间进行比较时,这个价格可以忽略不计。

我你是开发者为生,你的公司不会给你买,要么换公司,要么自己买。

I've used ANTS Profiler and I can join the others with recommendation.

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.

揪着可爱 2024-07-20 13:59:58

为了分析大型复杂 UI 应用程序,您通常需要一组工具和方法。 我将概述我最近在一个项目中使用的用于提高 .Net 2.0 UI 应用程序性能的方法和工具。

首先,我采访了用户并亲自研究了用例,得出了目标用例列表,突出显示了系统性能较差的区域。 也就是说,我不想花费 n 个工作日来优化一个几乎从未使用过但速度非常慢的功能。 然而,我想花时间优化一个有点迟缓但每天调用 1000 次的功能,等等。

一旦确定了候选用例,我就用我自己的轻量级日志记录类(我使用了一些高性能计时器和自定义日志记录解决方案,因为需要亚毫秒精度)。 不过,您也许可以摆脱 log4net 和时间戳的困扰。 我检测代码的原因是,有时读取您自己的日志比读取分析器的输出更容易。 出于多种原因,我需要两者(例如,使用探查器测量 .Net 用户控件布局并不总是那么简单)。

然后,我使用 ANTS 分析器运行我的检测代码并分析用例。 通过结合 ANTS 配置文件和我自己的日志文件,我很快就能发现我们的应用程序的问题。

我们还对服务器和 UI 进行了分析,并能够计算出在 UI 上花费的时间、在线路上花费的时间、在服务器上花费的时间等的细目。

还值得注意的是,1 次运行是不够的,并且第一次运行通常值得丢弃。 让我解释一下:PC 负载、网络流量、JIT 编译状态等都会影响特定操作所需的时间。 一个简单的策略是测量一个操作 n 次(比如 5 次),丢弃最慢和最快的运行,分析剩余配置文件。

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.

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.

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).

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.

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.

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.

埋情葬爱 2024-07-20 13:59:58

Eqatec profiler 是一款可爱的小型分析器,免费且易于使用。 就功能而言,它可能不会与 Ants profiler 的“哇”因素相媲美,但在我看来,它仍然非常酷,值得一看。

Eqatec profiler is 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.

谎言月老 2024-07-20 13:59:58

我只是设置了断点,视觉会告诉你断点之间已经过去了多少毫秒。 所以你可以手动找到它。

i just set breakpoints, visual will tell you how many ms between breakpoint has passed. so you can find it manually.

情何以堪。 2024-07-20 13:59:58

使用分析器ANTS 需要花钱,但非常好。

Use a profiler. ANTS costs money but is very nice.

冷心人i 2024-07-20 13:59:58

ANTS Profiler 非常好。

ANTS Profiler is very good.

留一抹残留的笑 2024-07-20 13:59:58

如果你不想付费,较新的 VS 版本带有分析器,但说实话,它看起来不太好。 ATI/AMD 制作了一个免费的分析器...但它不太用户友好(对我来说,我无法从中获得任何有用的信息)。

我给出的建议是用代码对函数调用进行计时。 如果它们很快,并且您没有高精度计时器,或者由于多种原因(例如,每个 x 调用构建某种缓存),调用的速度会有所不同,请尝试运行每个 x10000 次或其他内容,然后除以结果因此。 对于某些代码部分来说,这可能并不完美,但如果您无法找到一个好的、免费的第三方解决方案,那么除非您愿意付费,否则它几乎就剩下什么了。

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).

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.

多孤肩上扛 2024-07-20 13:59:58

另一种选择是英特尔的 VTune

Yet another option is Intel's VTune.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文