使用内联会降低性能吗?

发布于 2024-12-13 07:07:48 字数 34 浏览 2 评论 0原文

在某些特定情况下内联函数是否会降低应用程序的整体性能?

Can making function inline in some specific cases lower the overall performance of application?

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

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

发布评论

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

评论(2

≈。彩虹 2024-12-20 07:07:48

内联(特别是大型函数)会增加代码大小,从而影响缓存性能并降低整体性能。

Inlining (particularly large functions) can increase the code size so that cache performance is affected and overall performance decreases.

瞳孔里扚悲伤 2024-12-20 07:07:48

内联适用于所有支持它的语言。

值得内联的函数通常又短又简单,内联又长又复杂的函数是没有用的,但是像属性 getter 或执行一些算术计算的函数在内联时会获得很大的性能提升。

在 C 和 C++ 等语言中,当您指定 inline 关键字时,编译器会为您的方法选择最佳的内联策略,标记为 inline 的函数是“推荐内联”,但编译器会选择是否真正内联。
然而,只有当函数真正值得内联时,才应该放置 inline 关键字。

某些 C 和 C++ 编译器支持特殊关键字,例如 __forceinline 或其他一些特殊属性,这些属性向编译器表示“非常建议内联”,并且应谨慎使用这些关键字,因为内联大型且复杂的函数会降低性能。

Inlining works with every language that supports it.

The functions that deserve inlining are usually short and simple, it's unuseful to inline long and complicated functions, but things like property getter or functions that performs some arithmetical calculations gets a lot of performance boost when inlined.

In languages like C and C++ the compiler choose the best inline strategy for your method when you specify inline keyword, a function marked inline is "recommended for inlining", but is the compiler that choose if is the case to inline for real or not.
However you should put the inline keyword only when the function deserves inlining for real.

Some C and C++ compilers supports special keywords like __forceinline or some other special attributes that say to the compiler "this is very recommended for inlining" and these keywords should be used with caution, since inlining big and complicated functions can lower down performances.

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