我有一个定制的循环缓冲区实现,它使用通过new []分配的普通数组,然后使用std::move将元素移动到数组中。下面是我的push()方法的实现:
void push(value_type&& value)
{
_content[_end] = std::move(value); // 9.2% of execution is spend here
increment(); // 0.6% here
}我要移动到数组中的对象基本上只是一个指针和一个std::unique_ptr
struct Task
{
Task()
{}
Function function;
Batch *batch;
};Function看起来是这样的:
class Function
{
public:
template<typename F>
Function(F&& f) :
_implementation(new ImplementationType<F>(std::move(f)))
{}
void operator() () { _implementation->Call(); }
Function() = default;
Function(Function&& other) :
_implementation(std::move(other._implementation))
{}
Function& operator=(Function&& other)
{
_implementation = std::move(other._implementation);
return *this;
}
Function(const Function&) = delete;
Function(Function&) = delete;
Function& operator= (const Function&) = delete;
private:
struct Base
{
virtual void Call() = 0;
virtual ~Base() {}
};
template<typename F>
struct ImplementationType : Base
{
ImplementationType(F&& f) :
function(std::move(f))
{}
void Call()
{
function();
}
F function;
};
std::unique_ptr<Base> _implementation;
};我在循环中反复调用ringbuffers push()方法来填充缓冲区中的任务,那里没有其他的计算。我希望std::move()的开销非常小,而且肯定不会占用我计算时间中最大的一部分。有人能指出我在这里做错事的正确方向吗?
发布于 2013-10-21 17:00:42
std::move本身在运行时什么也不做;它只是将其参数转换为一个适合传递给移动赋值操作符的rvalue。这项任务需要时间。
如果_content[_end]不是空的,那么重新分配唯一指针将删除旧对象。也许这就是浪费时间的原因?
https://stackoverflow.com/questions/19499655
复制相似问题