remove_if with boost :: bind是慢的 [英] remove_if with boost::bind is slow

查看:355
本文介绍了remove_if with boost :: bind是慢的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个std ::类的类,并希望删除标记为删除的条目。我使用std :: remove_if和擦除。

I have a std::list of classes and want to remove entries that are marked for delete. I am using std::remove_if and erase.

class MyClass
{
    bool isDone(MyData& myData)
    {
        return myData.isDone();
    }

    void removeIfDone(std::list<MyData>& myList)
    {
        std::list<MyData>::iterator it =
            remove_if(myList.begin(), myList.end(), 
                  boost::bind(&MyClass::isDone, this, _1));
        myList.erase(it, myList.end());
    }
};

我在一个小型处理器上运行,内存分配和重新分配非常昂贵。

I am running on a small processor for which memory allocation and deallocation is very expensive. This remove is calling new and delete thousands of times in my application.

我以前使用过 boost :: ref 当传递一个非平凡的变量作为绑定参数,但在这种情况下,我认为这可能是创建和破坏函子本身或复制它是导致的问题。

I have previously used boost::ref when passing a non-trivial variable as a bind parameter but in this case I think it is probably the creation and destruction of the functor itself or the copying of it which is causing the problem.

我想做一些像

boost::bind(&MyClass::isDone, boost::ref(this), boost::ref(_1));

我找不到正在创建和销毁的文档。所以我的简单问题是如何使这更有效率?

I can't find documentation on what is being created and destroyed. So my simple question is how do I make this more efficient?

推荐答案

尝试替换 std :: remove_if std :: list :: remove_if 。后者应该只复制来自前面和后面的元素的一些指针,而不是试图将元素移动到列表的末尾,这是您看到的多个分配的原因。另一个好处是,由于它是 std :: list 的成员函数,它实际上会删除(即擦除)与您的标准匹配的元素。

Try replacing the call to std::remove_if with std::list::remove_if. The latter should only copy some pointers from the preceding and succeeding elements, instead of trying to move the elements to the end of the list, which is the cause of the multiple allocations you're seeing. An additional benefit is that since it is a member function of std::list, it actually removes (i.e. erases) the elements matching your criterion.

class MyClass
{
    bool isDone(MyData& myData)
    {
        return myData.isDone();
    }

    void removeIfDone(std::list<MyData>& myList)
    {
        myList.remove_if( boost::bind( &MyClass::isDone, this, _1 ) );
    }
};

这篇关于remove_if with boost :: bind是慢的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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