C ++ 0x Lambda开销 [英] C++0x Lambda overhead

查看:363
本文介绍了C ++ 0x Lambda开销的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++ 0x(在VS2010下)使用lambda表达式有什么开销吗?

我知道使用函数对象会产生开销,到传递给STL算法的表达式。编译器优化表达式,消除看起来像一个函数调用吗?我开始真的喜欢lambda表达式,但我有点担心速度的惩罚。

Is there any overhead associated with using lambda expressions in C++0x (under VS2010)?
I know that using function objects incurs overhead, but I'm referring to expressions that are passed to STL algorithms, for example. Does the compiler optimize the expression, eliminating what seems to appear like a function call? I started to really like lambda expressions, but I'm a bit concerned about the speed penalty.

提前感谢!

推荐答案

你知道函数对象会产生开销吗?也许你应该重新检查你的事实。 :)

You "know" that function objects incur overhead? Perhaps you should recheck your facts. :)

与手动循环相比,使用带有函数对象的STL算法通常只有零开销。一个朴素的编译器将不得不在函子上重复调用 operator(),但这对于内联很简单,因此实际上开销为零。

There is typically zero overhead to using a STL algorithm with a function object, compared with a hand-rolled loop. A naive compiler will have to repeatedly call operator() on the functor, but that is trivial to inline and so in effect, the overhead is zero.

lambda表达式只是一个函数对象的语法糖。代码由编译器转换为函数对象,因此它也有零开销。

A lambda expression is nothing more than syntactic sugar for a function object. The code is transformed into a function object by the compiler, so it too has zero overhead.

这篇关于C ++ 0x Lambda开销的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆