如何检查内联是否发生

发布于 2024-11-07 21:39:12 字数 155 浏览 0 评论 0原文

我想在我的项目中大量使用inline来提高性能。

据我所知,编译器可能会应用内联,也可能不应用内联;这取决于编译器。

我不清楚我能做些什么来实现这一点,但在朝着这个方向前进之前,您是否知道一种方法来检查输出二进制文件中是否确实发生了内联?

I would like to massively use inline in my project to speed up performance.

As far as I know the compiler might apply inline or not; it is up to the compiler.

It is not clear to me what I can do to make this possible, but before going that direction, do you know a way to check that inlinining really occurred or not in the output binary?

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

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

发布评论

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

评论(5

箜明 2024-11-14 21:39:12

当内联函数未内联时,使用 gcc -Winline 获取警告。

使用 __attribute__ ((always_inline)) 强制内联函数。

话虽如此,但请注意,如果不明智地使用内联,可能会降低性能、缩短编译时间并导致代码膨胀。

Use gcc -Winline to get warnings when an inline function is not inlined.

Use __attribute__ ((always_inline)) to force functions to be inlined.

Having said that, be warned that you can screw up performance, compile time and get huge code bloat if you use inlining injudiciously.

人海汹涌 2024-11-14 21:39:12

如果您使用的是 MS 编译器,您可能需要启用警告 C4710 获取未内联函数的警告。

If you are using the MS compiler you might want to enable warning C4710 to get a warning for functions not inlined.

幻想少年梦 2024-11-14 21:39:12

使用 gcc -S 选项生成汇编器输出,然后在您喜欢的文本编辑器中检查输出。

但是,编译器通常比您更能判断内联何时会真正提高性能。不要太急于强迫;分析您的代码并查看内联实际上是否更快。

Use the gcc -S option to generate assembler output, and then inspect the output in your favourite text editor.

But, the compiler is often a better judge than you of when inlining will actually improve performance. Don't be too hasty to force it; profile your code and see if inlining actually is faster.

ま昔日黯然 2024-11-14 21:39:12

编译器在这方面可能比您更聪明,但是忽略这一点,假设您没有启用任何特殊的编译器标志,您可以转储名称列表并查找该函数是否已生成。

static int foo(int x)
{
  return(x*x);
}

main()
{
  int x=1;
  foo(x);
}

测试

not seth> gcc -o /tmp/foo /tmp/main1.c
not seth> nm /tmp/foo | grep foo
00000000004004c4 t foo
not seth> gcc -O -o /tmp/foo /tmp/main1.c
not seth> nm /tmp/foo | grep foo

The compiler is probably smarter about this than you are, but ignoring that, assuming you don't have any special compiler flags enabled, you can dump the name list and find if the function has been generated.

static int foo(int x)
{
  return(x*x);
}

main()
{
  int x=1;
  foo(x);
}

To test

not seth> gcc -o /tmp/foo /tmp/main1.c
not seth> nm /tmp/foo | grep foo
00000000004004c4 t foo
not seth> gcc -O -o /tmp/foo /tmp/main1.c
not seth> nm /tmp/foo | grep foo
要走就滚别墨迹 2024-11-14 21:39:12

inline 关键字实际上与优化关系不大。大多数编译器都会内联函数call(函数本身可能需要单独编译,例如,如果您将其地址放在其他地方),无论是否存在inline关键字或不。

事实上,即使一个被调用的函数位于另一个翻译单元中,聪明的链接器也可以在链接时内联它(MSVC 提供此功能作为“链接时代码生成”)。但它需要编译器和链接器之间的强有力的合作。

inline 关键字的存在理由是允许[非模板]函数打破单一定义规则,从而在头文件中定义。函数的实际内联将由编译器根据传递给它的各种启发式方法和优化标志来决定,而不是根据 inline 关键字来决定。

因此,大量使用内联可能对性能绝对没有任何影响。如果您担心性能,请使用分析器来确定您的程序将时间花在哪里(通常是您不期望的地方),并通过优化实际瓶颈来采取相应的行动。

The inline keyword has actually little to do with optimization. Most compilers will inline a function call (the function itself may have to be compiled separately, eg. if you take its address somewhere else) regardless of whether the inline keyword is present or not.

In fact, even if one called function is in another translation unit, a clever linker may inline it at link time (MSVC provides this feature as "link time code generation"). It requires strong cooperation between the compiler and the linker though.

The raison d'être of the inline keyword is to allow [non template] functions to break the One Definition Rule, and thus to be defined in header files. The actual inlining of the function will be decided by the compiler based on various heuristics and optimization flags passed to it, and not based on the inline keyword.

So massively using inline will probably do absolutely nothing about performance. If you're worried about performance, use a profiler to determine where your program spends its time (often where you don't expect it to), and act accordingly, by optimizing the actual bottleneck.

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