__always_inline和inline之间的区别 [英] Difference between __always_inline and inline

查看:633
本文介绍了__always_inline和inline之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

inline 指令有很好的解释-static-and-static-inline-function>另​​一个问题

There is a nice explanation of using inline instruction on another question

有人可以解释一下使用 inline是否有区别 __ always_inline 在头文件上?

Could anyone explain me if there is any difference using inline and __always_inline on a header file?

而且,当我更喜欢 __ always_inline 而不是 inline 或反之亦然?

And, when I would prefer __always_inline over inline or vice-versa?

推荐答案

这里没有答案真正回答了何时应该使用内联而不是始终内联的问题,但答案很简单。除非您确切地知道自己在做什么,并且了解内联要注释的功能的性能含义,否则您应该几乎总是使用内联,因为错误的内联决策在最坏的情况下会使程序变慢,并且包括在执行时内联您不应该内联,也不应该在应该内联时内联。一般来说,在大多数情况下,编译器在决定这一点时非常聪明,但这并不完美。如果是这样,则不需要inline关键字。的确,C语言对于inline关键字的确切含义深奥不胜,因此您应该在使用它之前先通读,但是最好是最终决定是否将函数尽可能内联到编译器。

None of the answers here really answer the question of when you should use inline instead of always-inline, but the answer is fairly simple. You should pretty much always use inline unless you know exactly what you are doing and understand the performance implications of inlining the function you are annotating, because a bad inlining decision can in the worst case make a program much slower, and that includes both inlining when you shouldn't inline, and not inlining when you should inline. Generally speaking the compiler is quite intelligent at deciding this in the majority of cases, but it's not perfect; if it were, there would be no need for the inline keyword. It's true that the C language is quite esoteric about what precisely the inline keyword means, so you should read up before you use it, but it's better to leave ultimate say on whether to inline a function to the compiler as much as possible.

话虽这么说,当您优化代码的关键部分时(如果您不关心性能,为什么要内联代码呢?),有时编译器会错误的决定。在这种情况下,可以使用noinline或alwaysinline纠正行为。但是,在做出此决定之前,您应始终使自己熟悉所讨论的功能的组装;过多的内联会增加代码大小和寄存器压力,并降低缓存位置。太少会使代码量级变慢。

That being said, when you are optimizing critical parts of the code (and if you don't care about performance, why are you inlining things?), sometimes the compiler will make the wrong decision. In this case, you can correct the behavior with noinline or alwaysinline. But you should always familiarize yourself with the assembly of the functions in question before making this decision; too much inlining can increase code size and register pressure, and decrease cache locality. Too little can make code orders of magnitude slower.

这篇关于__always_inline和inline之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆