避免 std::function 的开销
Avoiding the overheads of std::function
我想对(自定义(单链表中的元素运行一组操作。遍历链表和运行操作的代码很简单,但重复,如果复制/粘贴到任何地方,可能会出错。性能和谨慎的内存分配在我的程序中很重要,所以我想避免不必要的开销。
我想编写一个包装器来包含重复代码并封装将在链表的每个元素上发生的操作。由于操作中发生的函数各不相同,我需要捕获必须提供给操作的多个变量(在实际代码中(,因此我考虑使用 std::function
.此示例代码中完成的实际计算在这里毫无意义。
#include <iostream>
#include <memory>
struct Foo
{
explicit Foo(int num) : variable(num) {}
int variable;
std::unique_ptr<Foo> next;
};
void doStuff(Foo& foo, std::function<void(Foo&)> operation)
{
Foo* fooPtr = &foo;
do
{
operation(*fooPtr);
} while (fooPtr->next && (fooPtr = fooPtr->next.get()));
}
int main(int argc, char** argv)
{
int val = 7;
Foo first(4);
first.next = std::make_unique<Foo>(5);
first.next->next = std::make_unique<Foo>(6);
#ifdef USE_FUNC
for (long i = 0; i < 100000000; ++i)
{
doStuff(first, [&](Foo& foo){ foo.variable += val + i; /*Other, more complex functionality here */ });
}
doStuff(first, [&](Foo& foo){ std::cout << foo.variable << std::endl; /*Other, more complex and different functionality here */ });
#else
for (long i = 0; i < 100000000; ++i)
{
Foo* fooPtr = &first;
do
{
fooPtr->variable += val + i;
} while (fooPtr->next && (fooPtr = fooPtr->next.get()));
}
Foo* fooPtr = &first;
do
{
std::cout << fooPtr->variable << std::endl;
} while (fooPtr->next && (fooPtr = fooPtr->next.get()));
#endif
}
如果运行方式:
g++ test.cpp -O3 -Wall -o mytest && time ./mytest
1587459716
1587459717
1587459718
real 0m0.252s
user 0m0.250s
sys 0m0.001s
而如果运行为:
g++ test.cpp -O3 -Wall -DUSE_FUNC -o mytest && time ./mytest
1587459716
1587459717
1587459718
real 0m0.834s
user 0m0.831s
sys 0m0.001s
这些计时在多次运行中相当一致,并且在使用 std::function
时显示 4 倍乘数。有没有更好的方法可以做我想做的事情?
使用模板:
template<typename T>
void doStuff(Foo& foo, T const& operation)
对我来说,这给了:
mvine@xxx:~/mikeytemp$ g++ test.cpp -O3 -DUSE_FUNC -std=c++14 -Wall -o mytest && time ./mytest
1587459716
1587459717
1587459718
real 0m0.534s
user 0m0.529s
sys 0m0.005s
mvine@xxx:~/mikeytemp$ g++ test.cpp -O3 -std=c++14 -Wall -o mytest && time ./mytest
1587459716
1587459717
1587459718
real 0m0.583s
user 0m0.583s
sys 0m0.000s
函数对象的权重相当重,但在有效负载非常大(>10000 个周期(或需要多态(例如在通用作业调度程序中(的情况下使用。
它们需要包含可调用对象的副本,并处理它可能引发的任何异常。
使用模板可以使您更接近金属,因为生成的代码经常被内联。
template <typename Func>
void doStuff(Foo& foo, Func operation)
{
Foo* fooPtr = &foo;
do
{
operation(*fooPtr);
} while (fooPtr->next && (fooPtr = fooPtr->next.get()));
}
编译器将能够查看函数内部并消除冗余。
在 Golbolt 上,您的内环变为
.LBB0_6: # =>This Loop Header: Depth=1
lea edx, [rax + 7]
mov rsi, rcx
.LBB0_7: # Parent Loop BB0_6 Depth=1
add dword ptr [rsi], edx
mov rsi, qword ptr [rsi + 8]
test rsi, rsi
jne .LBB0_7
mov esi, eax
or esi, 1
add esi, 7
mov rdx, rcx
.LBB0_9: # Parent Loop BB0_6 Depth=1
add dword ptr [rdx], esi
mov rdx, qword ptr [rdx + 8]
test rdx, rdx
jne .LBB0_9
add rax, 2
cmp rax, 100000000
jne .LBB0_6
作为奖励,如果您没有使用链表,循环可能会完全消失。
相关文章:
- "error: no matching function for call to"构造函数错误
- 实现无开销push_back的最佳方法是什么
- 调用专用模板时出错"no matching function for call to [...]"
- 库函数需要一个 std::function<void(void)>,如何传入类函数?
- Confusion: decltype vs std::function
- 为什么 std::function 可以作为 std::not2 的参数?
- 'max'匹配'std::function<const int &(const int &, const int &)>'无过载
- 传递给std::function template的template参数究竟代表什么
- 将带有unique_ptr的可变 lambda 传递给 const&std::function
- 绑定派生类方法C++从实例范围之外的分隔 std::function 变量调用
- "no matching function for call to 'Vector::Vector'"错误
- 别名模板的专业化 C++11 中没有开销的最佳替代方案
- 如何在向量中删除 std::function<void()>?
- C++标准是否允许<double>在没有开销的情况下实现 std::可选
- 不断"Attempting to reference a deleted function"
- 将函数包装器转换为 std::function
- 类型擦除的std::function与虚拟函数调用的开销
- C++ std::function 对于类 exept 的所有实例都是空的(只有 Visual2019 编译器问题)
- 避免 std::function 的开销
- parallel_fo(Inter-TBB)上是否存在与我们在std::function上看到的开销类似的开销