使用 Lambda 时,通过捕获子句按值传递还是通过作为参数传递来传递性能更高
When using Lambdas, is it more performant to pass by value through the capture clause or through passing as a parameter?
这个问题刚刚浮现在我的脑海中,我真的不知道该怎么弄清楚。
让我告诉你我的意思:
int x = 1;
auto lambda1 = [x](){
// do something with x, not specified here though
}
auto lambda2 = [](int x){
// do something with x, not specified here though
}
lambda1();
lambda2(x);
假设在给定时间我们只有 lambda1 或 lambda2。
在这种情况下,哪个功能会更快?我很确定差异很小,如果有任何差异的话,但这引起了我的兴趣,我真的很想知道!
问我们是否只使用一个 int 可能非常愚蠢,但在更大比例的 lambda 中可能存在可测量的差异。
第一个翻译为
struct _ {
int x;
_(int x_): x(x_) {}
void operator()() const {...}
};
第二个翻译为
struct _ {
_() = default;
void operator()(int x) const {...}
};
前者可能在关闭施工现场周围产生各种影响*,后者可能在关闭呼叫现场周围产生完全相同的影响*。
*-取决于
相关文章:
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 使用指向成员的指针将成员函数作为参数传递
- 如何将参数传递给正在使用模板的类
- 是否有C++编译器选项允许激进地删除所有函数调用,并将参数传递给具有空体的函数
- 修改函数中的指针(将另一个指针作为参数传递)
- 如何将部分流作为参数传递
- 我正在开发服务器,ip作为参数传递不起作用
- 将成员函数指针作为参数传递给模板方法
- 如何在C++中将迭代器作为函数参数传递
- 将附加参数传递给使用 beast::bind_front_handler 调用的函数
- 如何将一个类的函数作为另一个类的另一个函数的参数传递
- 将参数传递为"const"的奇怪效果
- 如何在 c++ 中将函数作为参数传递?
- 在 C++ 中将非指定类型作为参数传递的最佳方法?
- 使用引用与指针将数组作为参数传递
- 如何将成员函数作为回调参数传递给需要"typedef-ed"自由函数指针的函数?
- 将参数打包的参数传递到 std::queue 中,以便稍后使用不同的函数调用
- 为什么我不能将引用作为 std::async 的函数参数传递
- 使用 Lambda 时,通过捕获子句按值传递还是通过作为参数传递来传递性能更高
- 按值传递参数的性能