首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >std:移动()作为性能瓶颈?

std:移动()作为性能瓶颈?
EN

Stack Overflow用户
提问于 2013-10-21 16:15:48
回答 1查看 2.5K关注 0票数 5

我有一个定制的循环缓冲区实现,它使用通过new []分配的普通数组,然后使用std::move将元素移动到数组中。下面是我的push()方法的实现:

代码语言:javascript
复制
void push(value_type&& value)
{
    _content[_end] = std::move(value); // 9.2% of execution is spend here
    increment(); // 0.6% here
}

我要移动到数组中的对象基本上只是一个指针和一个std::unique_ptr

代码语言:javascript
复制
struct Task
{
    Task()
    {}

    Function function;
    Batch *batch;
};

Function看起来是这样的:

代码语言:javascript
复制
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()的开销非常小,而且肯定不会占用我计算时间中最大的一部分。有人能指出我在这里做错事的正确方向吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-10-21 17:00:42

std::move本身在运行时什么也不做;它只是将其参数转换为一个适合传递给移动赋值操作符的rvalue。这项任务需要时间。

如果_content[_end]不是空的,那么重新分配唯一指针将删除旧对象。也许这就是浪费时间的原因?

票数 12
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19499655

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档