c++ - 如何在 C++ 中优化检查 vector 中的元素

标签 c++ c++11

vector<unsigned> listNumbers; //listNumbers vector contains about 1000 million elements
vector<unsigned> storageVec; 
for(vector<unsigned>::iterator it=listNumbers.begin(), l=listNumbers.end(); it!=l; ++it)
{
            if(check_if_condition_satisfied(*it))
                   storageVec.push_back(*it);
}

vector “listNumbers”包含大约 10 亿个元素,我需要使用 check_if_condition_satisfied 检查“listNumbers”中的元素是否满足特定条件。不幸的是,一次检查 check_if_condition_satisfied 元素非常耗时。有什么方法可以并行化 C++11 中的检查

最佳答案

您可以使用 std::future std::async 轻松并行化计算。

未经测试的(伪)代码:

// You need to keep track of all futures you spawn. 
std::vector<std::future<bool>> futs;

// Spawn a future for every "block" that you want to check.    
futs.emplace_back(std::async(std::launch::async, 
    [&]{ return check_condition_range(listNumbers, 0, 10000); }));

futs.emplace_back(std::async(std::launch::async, 
    [&]{ return check_condition_range(listNumbers, 10000, 20000); }));

// (Ideally you would divide your range evenly depending on the number of threads you
// can spawn, and generate the futures in a loop.)

// ...

// "Accumulate" the results of every future.
// This blocks until all futures are done.
bool satisfied = true;
for(auto& f : futs)
{
    if(!f.get()) 
    {
        satisfied = false;
    }
}

Here's a simple example on wandbox .


如果您可以使用 C++17,您可以简单地使用:

std::all_of(std::execution::par, 
    std::begin(listNumbers), std::end(listNumbers),         
    check_if_condition_satisfied);

Sorry I also need to store the results in a common vector.

只需将您的 future 类型更改为 std::future<decltype(listNumbers)::iterator> ,这样每个 future 都会返回一个迭代器而不是 bool 值。

之后,您可以执行类似的操作:

std::vector<decltype(listNumbers)::iterator> result;
for(auto& f : futs) result.emplace_back(f.get());
// use `result`

关于c++ - 如何在 C++ 中优化检查 vector 中的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40585902/

相关文章:

C++ 覆盖继承的方法

c++ - 4 个有符号字节打包成 32 位无符号

c++ - 当鼠标指针在 QAbstractItemView 子类中绘制的项目上移动时如何显示工具提示?

c++ - 如何获取通用函数指针作为 C++ 类的私有(private)成员?

c++ - 未取消引用的迭代器是否超过了数组未定义行为的 "one past-the-end"迭代器?

c++ - VisualStudio2008 上的 std::vector 似乎没有得到最佳实现 - 太多的复制构造函数调用

c++ auto - 方便和困惑

c++ - 标准库容器在 GCC 中生成大量右值拷贝

c++ - 模板参数函数是否必须被视为潜在的 constexpr?

c++ - 外部参照环绕错误 : 'void' function returning a value