扩展方法是否太昂贵?

发布于 2024-09-14 05:04:53 字数 61 浏览 3 评论 0原文

如果我添加扩展方法,我的程序性能会损失多少?
它的工作速度会变慢还是加载速度会变慢?
谢谢。

How much will it cost for my programs performance if i add Extension methods to it?
Will it work slower or may be will load slower?
Thanks.

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

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

发布评论

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

评论(4

卖梦商人 2024-09-21 05:04:53

我的程序要花多少钱
如果我添加扩展方法,性能会提高
到它?它的工作速度会变慢还是可能会变慢
加载速度会变慢吗?

不会对性能造成影响。扩展方法实际上是静态方法。

How much will it cost for my programs
performance if i add Extension methods
to it? Will it work slower or may be
will load slower?

There will be no performance hit. Extension methods are really static methods.

给妤﹃绝世温柔 2024-09-21 05:04:53

扩展方法只是应用于静态方法的语法糖。

理论上,静态方法比非虚拟实例方法更快(不需要检查 null this),而非虚拟实例方法又比虚拟实例方法更快(不需要查找实现来称呼)。

从理论上讲,优化也可以消除这些差异。当涉及到扩展方法时,它在静态方面也被夸大了,因为无论如何,对象可能会以需要空检查的方式进行处理(无论是显式的还是隐式的使用方式)。

在实践中,无论扩展方法做什么,都有可能比扩展或实例产生更大的影响。

An extension method is just syntactic sugar applied to a static method.

In theory, a static method is faster than a non-virtual instance method (no need to check for null this) which in turn is faster than a virtual instance method (no need for looking up the implementation to call).

In theory also, optimisations could get rid of these differences. It's also overstated in terms of static when it comes to extension methods, since the object is probably going to be dealt with in such a way as requires a null-check (whether explicit or implicit to the ways it is use) anyway.

In practice, the chances are that whatever the extension method does will have much, much more of an impact that whether it's extension or instance.

半衬遮猫 2024-09-21 05:04:53

我在一个非常长的运行循环中使用一些整数扩展方法快速对其进行了性能测试,发现在我测试的情况下,性能通常是相同的。在调试或运行未优化的程序集时,扩展方法的运行速度大约是我的控制案例的两倍,这是有道理的,因为调试器无法内联这些方法并且必须跟踪调用堆栈等。

因此,在准备发布的程序集中,没有性能损失。

I quickly performance tested this using some integer extension methods in a REALLY long running loop and found that in the cases I tested, the performance was generally identical. When debugging or running unoptimized assemblies, extension methods ran about twice as slowly as my control cases, this makes sense because the debugger can't inline these methods and has to keep track of the callstack, etc.

So, in an assembly ready for release, no performance penalty.

善良天后 2024-09-21 05:04:53

它当然不会加载得更慢,因为加载程序不依赖于程序中的执行路径。

至于跑得慢一点?只有测量延伸法和非延伸法所花费的时间才能确定。我怀疑即使实际上可以测量,差异也不会很大。

重要的考虑因素应该始终是“扩展方法是否使我的代码更清晰、更易于理解和维护”。

It certainly won't load slower, as loading a program is not dependent on the execution paths in the program.

As for running more slowly? Only measuring the time taken for the extension method and the non-extension method will show for certain. I suspect the difference won't be significant even if it's actually measurable.

The important consideration should always be "does the extension method make my code clearer and easier to understand and maintain".

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