(如何)我可以内联特定函数调用? [英] (How) Can I inline a particular function call?
问题描述
让我们说,我有一个被调用在一个程序的多个部分功能。我们也可以说,我要的职能,这是在code的极端性能敏感的部分特定的呼叫(例如,一个循环,迭代千万次,其中每个微秒计数)。有没有一种方法,我可以强制编译器( GCC
在我的情况),内联单一,特别是函数调用,无需内嵌其他人呢?
Let's say that I have a function that gets called in multiple parts of a program. Let's also say that I have a particular call to that function that is in an extremely performance-sensitive section of code (e.g., a loop that iterates tens of millions of times and where each microsecond counts). Is there a way that I can force the complier (gcc
in my case) to inline that single, particular function call, without inlining the others?
编辑:让我做这个完全清楚:这个问题不是的强制的海湾合作委员会(或任何其他编译器)内联函数所有的呼叫;相反,它是关于请求编译器内嵌一个的特定呼叫的一个函数。
Let me make this completely clear: this question is NOT about forcing gcc (or any other compiler) to inline all calls to a function; rather, it it about requesting that the compiler inline a particular call to a function.
推荐答案
在C(而不是C ++)有表明函数应该内联的标准方法。这是只有特定厂商的扩展。
In C (as opposed to C++) there's no standard way to suggest that a function should be inlined. It's only vender-specific extensions.
不过你指定它,因为据我所知,编译器将总是尝试内联每个实例,因此使用该功能只有一次:
However you specify it, as far as I know the compiler will always try to inline every instance, so use that function only once:
原文:
int MyFunc() { /* do stuff */ }
更改为:
inline int MyFunc_inlined() { /* do stuff */ }
int MyFunc() { return MyFunc_inlined(); }
现在,在您希望它内联,想去的地方使用 MyFunc_inlined()
Now, in theplaces where you want it inlined, use MyFunc_inlined()
请注意:在上面的内联的关键字只是出于某种语法GCC使用强制的内联的占位符。如果H2CO3的删除答案是信任的,这将是:
Note: "inline" keyword in the above is just a placeholder for whatever syntax gcc uses to force an inlining. If H2CO3's deleted answer is to be trusted, that would be:
static inline __attribute__((always_inline)) int MyFunc_inlined() { /* do stuff */ }
这篇关于(如何)我可以内联特定函数调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!