gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 01:09:18 25 4
gpt4 key购买 nike

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com