2010-07-15 70 views
11

当使用GCC 4.4或MSVC编译C++时,是否有可能让函数内联时编译器发出消息?找出哪些函数被内联

+0

好问题,但我很好奇,你的动机。 – Daniel 2010-07-15 00:59:33

+0

我重新构造的代码将大量代码从头文件移动到cpp文件,现在运行得更慢。我想看看内联函数的数量是否有所不同 – 2010-07-15 01:14:36

+0

你是如何建设的?发布还是调试?你为Visual Studio指定了什么/ OPT? – 2010-07-15 01:49:31

回答

2

随着G ++,我不认为你可以使G ++报告,但你可以检查与任何工具,可以显示符号,nm例如生成的二进制文件:

#include <iostream> 
struct T { 
     void print() const; 
}; 
void T::print() const { std::cout << " test\n" ; } 
int main() 
{ 
     T t; 
     t.print(); 
} 

~ $ g++ -O3 -Wall -Wextra -pedantic -o test test.cc 
~ $ nm test | grep print 
0000000000400800 t _GLOBAL__I__ZNK1T5printEv 
0000000000400830 T _ZNK1T5printEv 

VS

#include <iostream> 
struct T { 
     void print() const { std::cout << " test\n" ; } 
}; 
int main() 
{ 
     T t; 
     t.print(); 
} 
~ $ g++ -O3 -Wall -Wextra -pedantic -o test test.cc 
~ $ nm test | grep print 

(在第二种情况下没有来自nm的输出)

编辑: 另外,分析器可能是有用的。 gprof的节目,在这两个例子:

0.00  0.00  0.00  1  0.00  0.00 global constructors keyed to _ZNK1T5printEv 
0.00  0.00  0.00  1  0.00  0.00 T::print() const 

主场迎战刚刚

0.00  0.00  0.00  1  0.00  0.00 global constructors keyed to main 
+5

FWIW具有相同的性能,这取决于函数的使用方式,编译器可能会内联一些实例而不是其他人。如果程序足够大,则需要更复杂的东西。 – Cogwheel 2010-07-15 03:04:47

+0

确实。我想知道是否有任何配置文件报告类似的情况(gprof似乎没有)。可能是一个有用的小工具来编写。 – Cubbi 2010-07-15 03:30:27

+0

谢谢。我可以在编译的二进制文件中看到不同之处。仅头部版本似乎已内嵌更多功能。我之前在gprof上运行过(用-pg编译),两者之间差别不大。我只是在callgrind上运行这些程序,并且只有头文件似乎内嵌了很多函数,因为它们不会显示在callgrinds输出中。 – 2010-07-15 04:06:23