Lambda是否像C ++中的函数一样内联?

编程入门 行业动态 更新时间:2024-10-26 06:28:28
本文介绍了Lambda是否像C ++中的函数一样内联?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

像简单的标准函数那样,编译器是否可以内联lambda函数来提高效率?

Can/does the compiler inline lambda functions to increase efficiency, as it might with simple standard functions?

例如

std::vector<double> vd; std::for_each(vd.begin(), vd.end(), [](const double d) {return d*d;});

还是因为缺乏优化而导致效率降低?

Or is there loss of efficiency caused by lack of optimisation?

第二个问题:在哪里可以检查我使用的编译器是否优化了内联函数的调用,这些调用已发送给算法?我的意思是,如果将一个函数(不是函数对象)发送给算法,则最后一个获取指向该函数的指针,而某些编译器会优化指向内联函数的指针,而其他编译器则不会.

A second question: where I can check if the compiler I use has optimised calls of inline functions, which are sent to an algorithm? What I mean is, if a function—not a function object—is sent to an algorithm, the last one gets a pointer to the function, and some compilers optimize pointers to inline functions and others don't.

推荐答案

在简单的情况下,例如您的示例,您应该期望lambda的性能要优于函数指针,请参见

In simple cases, like your example, you should expect better performance with lambdas than with function pointers, see

为什么与普通函数相比,编译器可以更好地优化lambda?

正如其他人已经指出的那样,不能保证您的呼叫会被内联,但是您有更多的机会使用lambda.检查呼叫是否已内联的一种方法是检查生成的代码.如果使用的是gcc,请将-S标志传递给编译器.当然,它假定您可以理解汇编代码.

As others have already pointed out, there is no guarantee that your call will be inlined but you have better chances with lambdas. One way of checking whether the call has been inlined is to check the generated code. If you are using gcc, pass the -S flag to the compiler. Of course, it assumes that you can understand the assembly code.

2018年9月11日更新: Vipul Kumar 指出了两个编译器标志在他的编辑中.

Update on Sep 11, 2018: Vipul Kumar pointed out two compiler flags in his edit.

GCC -Winline

GCC -Winline

警告是否不能内联声明为内联的函数.即使使用此选项,编译器也不会警告系统头文件中声明的内联函数失败.

Warn if a function that is declared as inline cannot be inlined. Even with this option, the compiler does not warn about failures to inline functions declared in system headers.

编译器使用各种启发式方法来确定是否内联函数.例如,编译器考虑了要内联的函数的大小以及当前函数中已经完成的内联量.因此,源程序中看似微不足道的更改可能导致-Winline产生的警告出现或消失.

The compiler uses a variety of heuristics to determine whether or not to inline a function. For example, the compiler takes into account the size of the function being inlined and the amount of inlining that has already been done in the current function. Therefore, seemingly insignificant changes in the source program can cause the warnings produced by -Winline to appear or disappear.

据我了解,如果您的函数未内联声明,则此编译器标志很可能无有用.不过,很高兴知道它的存在,并且部分回答了您的第二个问题.

As I understand this, if your function is not declared inline, this compiler flag is most likely not helpful. Nevertheless it is good to know it exists and it partly answers your second question.

他指出的另一个标志是:

The other flag that he pointed out is:

Clang -Rpass=inline

Clang -Rpass=inline

发布优化报告的选项

Options to Emit Optimization Reports

优化报告从高层次跟踪所有主要决策 由编译器转换完成.例如,当内衬 决定将函数foo()内联到bar()[...]

Optimization reports trace, at a high-level, all the major decisions done by compiler transformations. For instance, when the inliner decides to inline function foo() into bar() [...]

我自己还没有使用过,但是根据文档,它可能对您的用例有用.

I haven't used this one myself but based on the documentation it might be useful for your use case.

每当重要时,我都会亲自检查生成的程序集.

I personally check the generated assembly whenever it is that important.

更多推荐

Lambda是否像C ++中的函数一样内联?

本文发布于:2023-07-16 23:07:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1126881.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:内联   函数   Lambda

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!