测量函数、类和进程速度的最佳方法

发布于 2024-10-15 08:08:39 字数 111 浏览 6 评论 0 原文

衡量函数和类速度的最佳方法是什么?对于每个功能都有各种解决方案,我想知道如何测量运行速度并使用最佳解决方案优化我的共享类。

另外,如何衡量 SQL 速度,例如存储过程、选择、视图等之间的差异?

What is the best way to measure speed of functions and classes? For every function there are various solutions and I'd like to know how to measure running speed and optimize my shared classes with the best solution possible.

Also how do you measure SQL speed, for example differences between stored procedures, selects, views and etc.?

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

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

发布评论

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

评论(3

能怎样 2024-10-22 08:08:39

你有两个选择。

使用System.Diagnostics.Stopwatch 用于特定方法。

这是 .NET 中的高分辨率计时器,您可以将其用于代码的特定部分。

如果需要测量整个应用程序的性能,请使用探查器。

您可以使用 Visual Studio Ultra 版本的内置探查器,或 EQATEC

You have two options.

Use System.Diagnostics.Stopwatch for specific methods.

This is a high resolution timer in .NET which you can use for specific parts of your code.

Use a profiler if you need to measure performance for your entire application.

You can use the build in profiler of the Ultra edition of Visual Studio, or a tool like EQATEC.

喜爱皱眉﹌ 2024-10-22 08:08:39

您最好使用分析器。测量挂钟执行时间(例如由 Stopwatch 类完成)的问题在于,它受到许多远远超出您控制范围的因素的影响。仅举几个此类因素,包括网络和其他 I/O 延迟以及影响应用程序和线程的相对优先级的操作系统调度程序决策。如果您在虚拟化环境中运行,这也会对毫秒范围的挂钟时间测量产生相对较大的影响。探查器并不完美,但它可以让您更好地了解执行代码实际花费了多少时间。

除此之外,一个好的分析器通常还可以为您提供其他有用的指标,例如代码在执行过程中使用了多少内存。

You're best off using a profiler. The problem with measuring wallclock execution time (such as done by the Stopwatch class) is that it is influenced by many factors far out of your control. To mention just a few such factors, there's network and other I/O latency, and operating system scheduler decisions which influence the relative priority of applications and threads. If you are running inside a virtualized environment, that can also have a comparatively large effect on millisecond-range wallclock time measurements. The profiler won't be perfect, but it will give you a better idea of how much time is actually spent executing your code.

Besides that, a good profiler can often give you other useful metrics as well, like how much memory your code uses during execution.

撧情箌佬 2024-10-22 08:08:39

最好不要混淆测量和优化的目标。它们是不同的任务。测量虽然有助于量化修复某些问题的结果,但无法告诉您要修复什么。

在测量方面,如果我想演示某事物的速度有多快,我需要一个受控的环境和一个好的秒表。然而,为了优化的目的,粗略的测量(比如运行 1000 次)和使用简单的计时器就足够了。

关于优化,我并不关心速度。
我担心不必要的活动。
不需要高速运行代码来找到它。

当任何程序运行时,它都会跟踪调用树。
优化包括删除尽可能多的叶子(指令)和尽可能多的水果(I/O)。
一个好方法是修剪整个树枝。

  • 在除了最小的程序之外的所有程序中,典型的优化机会都由调用点(调用函数的代码行,而不是函数本身)组成,当人们意识到有多少调用树从中萌芽时,实际上可以用另一个函数来完成这些调用点。方式。看似无辜的一行代码可能负责整个树的很大一部分,您也许可以简单地将其砍掉。

为了找到这些,我认为挂钟时间堆栈采样是最好的方法。这不一定是一个有效的过程,并且相当少量的样本与大量样本一样好(或更好)。
有必要重复执行此操作,因为任何给定的程序在第一次编写时都不只包含一次加速机会。它包含几个。
这是一个示例。

It's best not to confuse the goals of measuring and optimizing. They are different tasks. Measuring, while good for quantifying the results of fixing something, is poor at telling you what to fix.

On the subject of measuring, if I want to make a presentation of how fast something is, I want a controlled environment and a good stopwatch. However, for the purpose of optimizing, crude measurements (like running something 1000 times) and using a simple timer, are more than good enough.

On the subject of optimizing, I am not concerned with speed.
I am concerned with needless activity.
It is not necessary to run the code at high speed to find it.

When any program runs, it traces out a call tree.
Optimizing consists of removing as many leaves (instructions) and as much fruit (I/O) as possible.
A good way to do this is to prune whole branches.

  • In all but the smallest programs, the typical opportunities for optimization consist of call points (lines of code where functions are called, not the functions themselves) that, when one realizes how much of the call tree sprouts from them, could really be done another way. A single innocent-looking line of code could be responsible for a large fraction of the entire tree, and you might be able to simply chop it off.

To find those, I think wall-clock time stack sampling is the best way. It is not necessary for this to be an efficient process, and a rather small number of samples works just as well (or better) than a large number of samples.
It is necessary to do it repeatedly, because any given program, as first written, doesn't contain just one opportunity for speedup. It contains several.
Here's an example.

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