将内嵌GCC的一个函数,指针? [英] Will GCC inline a function that takes a pointer?
问题描述
我有对数据块进行操作的函数(比方说,一个 INT
),我想传递一个参考valule改到位。因此,我具备的功能:无效myFunction的为(int *的东西){...}
。当我使用它,我这样称呼它: myFunction的(安培; ANINT)
。
I have a function which operates on piece of data (let's say, an int
), and I want to change it in place by passing a reference to the valule. As such, I have the function: void myFunction(int *thing) { ... }
. When I use it I call it thus: myFunction(&anInt)
.
由于我的函数往往(但来自许多不同的地方)叫我关心它的性能。我已经重构它变成一个功能的原因是可测试性和code重用。
As my function is called frequently (but from many different places) I am concerned about its performance. The reason I have refactored it into a function is testability and code reuse.
编译器将能够优化功能,它内联直接在我的 ANINT
变量进行操作?
Will the compiler be able to optimize the function, inlining it to operate directly on my anInt
variable?
我希望您能在它的要求精神,这个问题(即我不是prematurely担心的优化,我很好奇的答案)。同样,我不想把它做成一个宏。
I hope you'll take this question in the spirit in which it's asked (i.e. I'm not prematurely worrying about optimisation, I'm curious about the answer). Similarly, I don't want to make it into a macro.
推荐答案
GCC是相当聪明的。考虑这个code片段:
GCC is quite smart. Consider this code fragment:
#include <stdio.h>
void __inline__ inc(int *val)
{
++ *val;
}
int main()
{
int val;
scanf("%d", &val);
inc(&val);
printf("%d\n", val);
return 0;
}
在之后的gcc -S -O3 test.c以
你会得到以下相关ASM:
After a gcc -S -O3 test.c
you'll get the following relevant asm:
...
call __isoc99_scanf
movl 12(%rsp), %esi
movl $.LC1, %edi
xorl %eax, %eax
addl $1, %esi
movl %esi, 12(%rsp)
call printf
...
正如你所看到的,有没有必要成为一个ASM专家看INC()调用已被转换为增量指令。
As you can see, there's no need to be an asm expert to see the inc() call has been converted to an increment instruction.
这篇关于将内嵌GCC的一个函数,指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!